JavaScript和原型继承 [英] JavaScript and prototype inheritance

查看:81
本文介绍了JavaScript和原型继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像我终于理解了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.


  1. Male.prototype = Object.create(Human.prototype) )原型链是 Male.prototype - > Human.prototype - > Object.prototype - > null

  2. Male 构造函数中,我使用 Human.call(这个,眼睛); 要调用一个超类,我必须再次在 Male 构造函数中传递它以将其传递给 Human 构造函数。这似乎很痛苦,有没有更简单的方法呢?

  3. 为什么有时候我会看到像 Male.prototype = new Human(); 这样的代码......这似乎是不正确的。当我们这样做时实际发生了什么?

  1. In the Male.prototype = Object.create(Human.prototype) would the prototype chain be Male.prototype --> Human.prototype --> Object.prototype --> null?
  2. In the Male constructor where I use Human.call(this, eyes); to call a super class, I have to pass eyes again in the Male constructor to pass it to the Human constructor. This seems like a pain, is there an easier way to do this?
  3. How come sometimes I see code like Male.prototype = new Human(); ... This seems to be incorrect. What is actually happening when we do that??


推荐答案

回答你的问题:


  1. 这是正确的。当您设置 Male.prototype = Object.create(Human.prototype)时,您将原型链设置为 Male.prototype - > Human.prototype - > Object.prototype - >空。然后当你创建 var Sethen = new Male 时,实例( Sethen )继承自这个原型链。

  2. 不,您需要手动将参数传递给基础构造函数。如果你想设置 this.eyes = eyes? 不盲:盲目男性函数本身,但这只是代码重复。你正在做的是正确的。

  3. 这是旧的做事方式(在 Object.create 标准化之前)。我建议你不要使用它。对于大多数情况,它与新方法相同。但是,使用此方法,您还会在 Male.prototype 上获得不需要的属性,例如 eyes

  1. That's correct. When you set Male.prototype = Object.create(Human.prototype) you set the prototype chain as Male.prototype --> Human.prototype --> Object.prototype --> null. Then when you create var Sethen = new Male the instance (Sethen) inherits from this prototype chain.
  2. 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 the Male function itself but that would just be code duplication. What you're doing is correct.
  3. 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 like eyes on Male.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屋!

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