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?
Example:
function Foo () {
return something;
}
var foo = new Foo ();
If I'm not mistaken, if something
is a non-function primitive, this
will be returned. Otherwise something
is returned. Is this correct?
In other words, what values can something
take to cause (new Foo () instanceof Foo) === false
?
The exact condition is described on the [[Construct]]
internal property, which is used by the new
operator:
From the ECMA-262 3rd. Edition Specification:
13.2.2
[[Construct]]
When the
[[Construct]]
property for aFunction
objectF
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 originalObject
prototype object as described in 15.2.3.1.- Invoke the
[[Call]]
property ofF
, providingResult(1)
as thethis
value and providing the argument list passed into[[Construct]]
as the argument values.- If
Type(Result(6))
isObject
then returnResult(6)
.- Return
Result(1)
.
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.