Object.create将prototype.constructor更改为父构造函数,但在子实例化时,子构造函数运行 [英] Object.create changes prototype.constructor to parent constructor, but upon child instantiation, child constructor runs

查看:572
本文介绍了Object.create将prototype.constructor更改为父构造函数,但在子实例化时,子构造函数运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个例子来说明:

  //这是父类
function animal ){console.log('animal constructor')}

//允许动物走动
animal.prototype.walk = function(){console.log('animal walking')}

//创建子类
function cat(){console.log('cat constructor')}

// cat继承自动物
cat.prototype = Object.create(animal.prototype);

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

//创建一个cat对象
var myCat = new cat();

/ *输出:cat constructor * /

//但是,运行的构造函数不同于cat报告的原型
console.log(cat .prototype.constructor);

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

因此,请注意继承的主要工作方式如何,cat从父狗类继承了方法walk,并向子类添加了更多方法,如meow按预期工作。但是,当我创建一个cat的实例时,cat的构造函数运行,而cat.prototype.constructor则指向从dog继承的构造函数。



object.prototype.constructor的目的不是允许我们在声明对象后修改对象的构造函数,而不会清除对象的原型?在上面的例子中,不应该将存储在cat.prototype.constructor中的构造函数指向创建一个cat对象时运行的同一个构造函数?这种明显的歧义与如何在此源代码中运行此语句运行有关:

  //强制构造函数成为我们期望的结果
Class.prototype.constructor = Class;


解决方案

c $ c>属性只是原型对象的一个​​简单属性。在幕后,没有什么奇怪的事情发生。



所以,当你这样做:


$ b这是一个可以覆盖和重置的属性,但不会影响任何其他。 $ b

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

您将覆盖函数的整个原型属性 cat 具有源自动物原型对象的完整新对象。但由于构造函数属性只是原型对象的一个​​正常属性,它也将被覆盖。 p>

是的,当实现继承时,行

  Class.prototype。 constructor = Class;通常添加

来恢复构造函数属性到它的原始值,并以某种方式撤消由这个继承模式所做的损害。



在你的情况下,你需要添加以下行:

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


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;

解决方案

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

这篇关于Object.create将prototype.constructor更改为父构造函数,但在子实例化时,子构造函数运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆