您的位置:首页 > Web前端 > JavaScript

What's the returned value of javascript constructor function

2014-11-19 23:38 435 查看
Example:

function Foo(){
return something;
}
var a = new Foo();


The exact condition is described on the
[[Construct]]
internal
property, which is used by the
new
operator:

From the ECMA-262 3rd. Ediion Specification:

13.2.2
[[Construct]]


When the
[[Construct]]
property
for a
Function
object
F
is
called, the following steps are taken:

Create a new native ECMAScript object.

Set the
[[Class]]
property
of
Result(1)
to
"Object"
.

Get the value of the prototype property of
F
.

If
Result(3)
is
an object, set the
[[Prototype]]
property
of
Result(1)
to
Result(3)
.

If
Result(3)
is
not an object, set the
[[Prototype]]
property
of
Result(1)
to
the original
Object
prototype
object as described in 15.2.3.1.

Invoke the
[[Call]]
property
of
F
,
providing
Result(1)
as
the
this
value
and providing the argument list passed into
[[Construct]]
as
the argument values.

If
Type(Result(6))
is
Object
then
return
Result(6)
.

Return
Result(1)
.

Look at the steps 7 and 8, the new object will returned only if the type of
Result(6)
(the
value returned from the
F
constructor
function) is not an Object.
Steps 7 “Type(x) is Object” means “x instance of Object is true”, not “typeof(x) === ‘object’ is true”

Add one website for refer http://dmitrysoshnikov.com/ecmascript/javascript-the-core/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: