Why does the constructor change from Foo to Object after adding a prototype? How can I get access to the original constructor?
Code:
function Foo() {}
var foo1 = new Foo();
console.log('foo1: ' + foo1.constructor);
Foo.prototype = {}
var foo2 = new Foo();
console.log('foo2: ' + foo2.constructor);
Output:
foo1: function Foo() {}
foo2: function Object() {
[native code]
}
http://jsfiddle.net/vDCTJ/
It happens because you gave Foo a brand new object for its prototype, and you didn't set that object's "constructor" property.
Foo.prototype = { constructor: Foo };
Instantiated function objects get an object for their "prototype" property that's already initialized that way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With