JavaScript和原型继承 [英] JavaScript and prototype inheritance
问题描述
好像我终于理解了JavaScript继承以及如何正确地完成它。这是我的代码:
It seems as though I am finally understanding JavaScript inheritance and how it should be done properly. Here is my code:
function Human(eyes) {
this.eyes = eyes ? "Not blind" : "Blind";
}
Human.prototype.canSee = function () {
return this.eyes;
};
function Male(name, eyes) {
Human.call(this, eyes);
this.name = name;
}
Male.prototype = Object.create(Human.prototype);
var Sethen = new Male("Sethen", true);
console.log(Sethen.canSee()); //logs "Not blind"
根据我的理解,使用 Object。创建
来创建继承的原型对象要比使用 new
关键字好得多。这引起了一些问题。
From what I understand, using Object.create
to create your prototype object for inheritance is much better than using the new
keyword. This raises a couple questions in my head.
- 在
Male.prototype = Object.create(Human.prototype) )
原型链是Male.prototype - > Human.prototype - > Object.prototype - > null
? - 在
Male
构造函数中,我使用Human.call(这个,眼睛);
要调用一个超类,我必须再次在Male
构造函数中传递它以将其传递给Human
构造函数。这似乎很痛苦,有没有更简单的方法呢? - 为什么有时候我会看到像
Male.prototype = new Human();
这样的代码......这似乎是不正确的。当我们这样做时实际发生了什么?
- In the
Male.prototype = Object.create(Human.prototype)
would the prototype chain beMale.prototype --> Human.prototype --> Object.prototype --> null
? - In the
Male
constructor where I useHuman.call(this, eyes);
to call a super class, I have to pass eyes again in theMale
constructor to pass it to theHuman
constructor. This seems like a pain, is there an easier way to do this? - How come sometimes I see code like
Male.prototype = new Human();
... This seems to be incorrect. What is actually happening when we do that??
推荐答案
回答你的问题:
- 这是正确的。当您设置
Male.prototype = Object.create(Human.prototype)
时,您将原型链设置为Male.prototype - > Human.prototype - > Object.prototype - >空
。然后当你创建var Sethen = new Male
时,实例(Sethen
)继承自这个原型链。 - 不,您需要手动将参数传递给基础构造函数。如果你想设置
this.eyes = eyes? 不盲:盲目
在男性
函数本身,但这只是代码重复。你正在做的是正确的。 - 这是旧的做事方式(在
Object.create
标准化之前)。我建议你不要使用它。对于大多数情况,它与新方法相同。但是,使用此方法,您还会在Male.prototype
上获得不需要的属性,例如eyes
。
- That's correct. When you set
Male.prototype = Object.create(Human.prototype)
you set the prototype chain asMale.prototype --> Human.prototype --> Object.prototype --> null
. Then when you createvar Sethen = new Male
the instance (Sethen
) inherits from this prototype chain. - No, you need to manually pass the parameters to the base constructor function. If you want to could set
this.eyes = eyes ? "Not blind" : "Blind"
within theMale
function itself but that would just be code duplication. What you're doing is correct. - That was the old way of doing things (before
Object.create
was standardized). I would recommend that you don't use it. For most cases it's the same as the new method. However using this method you also get unwanted properties likeeyes
onMale.prototype
.
当我们执行 Male.prototype = new Human
时,我们创建一个的新实例
并将其分配给 Male.prototype
。因此原型链是 Male.prototype - > Human.prototype - > Object.prototype - >空
。但是我们也得到了我们不需要的 Male.prototype.eyes
属性。这应该只属于男性
的实例。
When we do Male.prototype = new Human
we create a new instance of Human
and assign it to Male.prototype
. Hence the prototype chain is Male.prototype --> Human.prototype --> Object.prototype --> null
. However we also get the property Male.prototype.eyes
which we don't require. That should only belong to the instance of Male
.
我建议你阅读我的博文。这是关于原型继承的一个非常好的入门: Aadit M Shah |为什么原型继承很重要
I would recommend you read my blog post. It's a really good primer on prototypal inheritance: Aadit M Shah | Why Prototypal Inheritance Matters
这篇关于JavaScript和原型继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!