Why is the constructor not a constructive function?

Consider the following snippet:

​f = function() {};
f.prototype = {};
thing = new f;

I was surprised to see that thing.constructor​- Object(). (See Violin here .)

Why not a thing.constructorfunction f?

+5
source share
1 answer

Since you completely replaced the original object of the prototypeobject fwith a simple object. It was an original object prototypethat contained a link to fthrough a property .constructor.

The constructor of an object created using the object literal syntax will be the constructor Object.

To return it, you need to put it there manually.

f = function() {};
f.prototype = {};
f.prototype.constructor = f;
thing = new f;

.constructor .

, Object.

delete f.prototype.constructor;

console.log(thing.constructor); // Object
+9

All Articles