当使用 new
What are the exact circumstances for which a return statement in Javascript can return a value other than this when a constructor is invoked using the new keyword?
示例:
function Foo () { return something; } var foo = new Foo ();如果我没有错误,如果 something 是一个非函数原语,将返回 this 。否则返回 something 。这是正确的吗?
If I'm not mistaken, if something is a non-function primitive, this will be returned. Otherwise something is returned. Is this correct?
IOW,什么值可以某事引起 ()instanceof Foo)=== false ?
IOW, what values can something take to cause (new Foo () instanceof Foo) === false?
推荐答案code> [[Construct]] 内部属性,由 new 运算符:
The exact condition is described on the [[Construct]] internal property, which is used by the new operator:
版本规格:
13.2.2 [[Construct]]
[[Construct]] 属性函数对象 F 是调用,执行以下步骤:
When the [[Construct]] property for a Function object F is called, the following steps are taken:
查看步骤7和8,只有当类型 Result(6)(从 F 构造函数函数)是不一个对象。
Look at steps 7 and 8, the new object will be returned only if the type of Result(6) (the value returned from the F constructor function) is not an Object.
更多推荐
构造函数返回什么值可以避免返回?
发布评论