Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

javascript - Object.create changes prototype.constructor to parent constructor, but upon child instantiation, child constructor runs

I've created an example to illustrate:

// this is the parent class
function animal() { console.log('animal constructor') }

// allow animals to walk
animal.prototype.walk = function() { console.log('animal walking') }

// create child class
function cat() { console.log('cat constructor') }

// cat inherits from animal
cat.prototype = Object.create(animal.prototype);

// let cats meow
cat.prototype.meow = function() { console.log('meow') }

// create a cat object
var myCat = new cat();

/* output: cat constructor */

// yet, the constructor that ran is different than what the prototype for cat reports
console.log(cat.prototype.constructor);

/* output: function animal() { console.log('animal constructor') } */

So note how inheritance mostly worked as expected, cat inherited the method 'walk' from its parent dog class, and adding further methods to the child class such as meow works as expected. However, when I create an instance of cat, the constructor for cat runs, whereas cat.prototype.constructor points to the constructor "inherited" from dog.

Isn't the purpose of object.prototype.constructor to allow us to modify the constructor of an object after the object has been declared without wiping out the prototype for the object? In the above example, shouldn't the constructor stored in cat.prototype.constructor point to the same constructor that runs when creating a cat object? Does this apparent ambiguity have something to do with how in this source code this statement is run:

// Enforce the constructor to be what we expect
Class.prototype.constructor = Class;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The constructor property is just a simple property of the prototype object. There is nothing magically happening with it behind the scenes. It's a property that can be overridden and reset, but it doesn't affect anything else.

So when you do this:

cat.prototype = Object.create(animal.prototype);

you are overriding the entire prototype property of the function cat with a complete new object derived from the prototype object of animal. But since the constructor property is just a normal property of the prototype object, it will get overridden as well.

And yes, when implementing inheritance, the line

Class.prototype.constructor = Class;

is commonly added to restore the constructor property to its original value and to kind of undo the "damage" that was done by this inheritance pattern.

So in your case, you would need to add the following line:

cat.prototype = Object.create(animal.prototype);
cat.prototype.constructor = cat;  //  <---- add this

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...