Example:
function Foo(){
return something;
}
var a = new Foo();
The exact condition is described on the [[Construct]] internal property, which is used by the newoperator:
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 ofResult(1)to"Object". - Get the value of the prototype property of
F. - If
Result(3)is an object, set the[[Prototype]]property ofResult(1)toResult(3). - If
Result(3)is not an object, set the[[Prototype]]property ofResult(1)to the originalObjectprototype object as described in 15.2.3.1. - Invoke the
[[Call]]property ofF, providingResult(1)as thethisvalue and providing the argument list passed into[[Construct]]as the argument values. - If
Type(Result(6))isObjectthen returnResult(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.
Add one website for refer http://dmitrysoshnikov.com/ecmascript/javascript-the-core/

本文深入解析JavaScript中构造函数的工作原理及使用方式。通过ECMAScript规范详细解释了使用new操作符创建实例的过程,并强调了返回值类型对于实例化的影响。
762

被折叠的 条评论
为什么被折叠?



