function Person(name, age, gender) {
this.name = name; // run
}
function PersonSafe(name) {
if (!(this instanceof arguments.callee))
return new PersonSafe(name)
this.name = name; // run
}
var qux = Person('qux');
console.log('Person qux: ', qux);
console.log('[[global]] name: ', name);
var quxSafe = PersonSafe('qux');
console.log('PersonSafe qux: ', quxSafe);
A comparison of the two situations when the constructor
is called without the new
keyword.
I don't know why the results of running the two codes are different.
The body
ofPerson()
andPersonSafe()
functions are different,
but the line (this.name = name;
) thatqux
and quxSafe
will execute is the same.
So... Why are the results different?