Calling a function with `new` normally returns the newly created object. An explicit primitive return, such as a number, is ignored; an explicit object replaces that new instance. section 4.2.3
Listing 4.8. Constructors returning primitive values
function Ninja() { #A
this.skulk = function () {
return true;
};
return 1; #B
}
assert(Ninja() === 1, #C
"Return value honored when not called as a constructor"); #C
var ninja = new Ninja(); #D
assert(typeof ninja === "object", #E
"Object returned when called as a constructor"); #E
assert(typeof ninja.skulk === "function", #E
"ninja object has a skulk method"); #E
The constructor returns `1`, but `new Ninja()` still produces an object with its `skulk` method. section 4.2.3
Listing 4.9. Constructors explicitly returning object values
var puppet = { #A
rules: false #A
}; #A
function Emperor() { #B
this.rules = true; #B
return puppet; #B
}
var emperor = new Emperor(); #c
assert(emperor === puppet, #D
"The emperor is merely a puppet!"); #D
assert(emperor.rules === false, #D
"The puppet does not know how to rule!"); #D
`Emperor` initializes the new instance, then returns `puppet`; the constructor call’s result is that existing object. section 4.2.3
