Consider the following snippet:
f = function() {};
f.prototype = {};
thing = new f;
I was surprised to see that thing.constructor
is Object()
. (See fiddle here.)
Why isn't thing.constructor
the function f
?
Because you've entirely replaced the original prototype
object of f
with a plain object. It was the original prototype
object that held the reference to f
via the .constructor
property.
The constructor of an object created using object literal syntax will be the Object
constructor.
To get it back, you'd need to put it there manually.
f = function() {};
f.prototype = {};
f.prototype.constructor = f;
thing = new f;
This will shadow the .constructor
property on in the prototype chain of the new prototype object.
If you delete that property, you'll get Object
again.
delete f.prototype.constructor;
console.log(thing.constructor); // Object
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