JavaScript 继承:我的派生成员什么时候在哪里?

发布于 2024-12-31 23:29:10 字数 364 浏览 0 评论 0原文

看一下下面的代码:

function Primate() {
    this.prototype = Object;
    this.prototype.hairy = true;
}

function Human() {
    this.prototype = Primate;
}

new Human();

当您检查 new Human() 时,没有 hairy 成员。我希望会有一个。我是否有另一种方式可以从 Primate 继承?涉及 Object.create() 的东西(ECMAScript5 可以在我的场景中使用)?

Take a look at the following code:

function Primate() {
    this.prototype = Object;
    this.prototype.hairy = true;
}

function Human() {
    this.prototype = Primate;
}

new Human();

When you inspect new Human(), there's no hairy member. I would expect there to be one. Is there another way I'm suppose to inherit from Primate? Something involving Object.create() (ECMAScript5 is fine to use in my scenario)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油饼 2025-01-07 23:29:10

在编写代码时,使用 new Human() 创建的对象将具有一个名为 prototype 的属性,其值是对 Primate 函数的引用。这显然不是你想要的(而且也不是特别特别)。

一些事情:

  • 您通常想要修改旨在用作构造函数的函数原型(使用new操作员)。换句话说,您希望在 Human 上设置 prototype(而不是在 Human实例上)。< /p>

  • 您分配给prototype的值应该是所需类型的实例(或者,如果不需要初始化工作,则为所需类型的prototype< /code>),而不是对其构造函数的引用。

  • 从来没有必要将Object(或Object实例)显式分配给函数的原型。这是隐式的。

您可能想要更像这样的内容:

function Primate() {
    this.hairy = true; 
}

function Human() {}
Human.prototype = new Primate();
Human.prototype.constructor = Human;

var h = new Human(); 

h 引用的 Human 有一个名为 hairy 的属性,其值为 true。

在前面的示例中,仅在调用 Primate 时才为 hairy 分配其值,这就是为什么 Human.prototype 必须分配一个 Human.prototype 的实例。代码>灵长类动物。可以这样写,这样就不需要这样的初始化。

例子:

function Primate() {}
Primate.prototype.hairy = true;

function Human() {}
Human.prototype = Primate.prototype;
Human.prototype.constructor = Human;

var h = new Human();

As your code is written, objects created using new Human() will have a property called prototype whose value is a reference to the Primate function. That's clearly not what you want (and it's not particularly special).

A few things:

  • You usually want to modify the prototype of a function that's intended to be used as a constructor (with the new operator). In other words, you want to set the prototype on Human (not on an instance of Human).

  • The value you assign to the prototype should be an instance of the desired type (or, if no initialization work is necessary, the desired type's prototype), not a reference to its constructor.

  • It's never necessary to explicitly assign Object (or Object instances) to a function's prototype. This is implicit.

You probably want something more like this:

function Primate() {
    this.hairy = true; 
}

function Human() {}
Human.prototype = new Primate();
Human.prototype.constructor = Human;

var h = new Human(); 

The Human referenced by h has a property called hairy whose value is true.

In the previous example, hairy is assigned its value only once Primate is invoked, which is why Human.prototype must be assigned an instance of Primate. This could instead be written so that no such initialization is necessary.

Example:

function Primate() {}
Primate.prototype.hairy = true;

function Human() {}
Human.prototype = Primate.prototype;
Human.prototype.constructor = Human;

var h = new Human();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文