使用继承时,构造函数和原型对象之间有什么区别吗? [英] Is there any difference between constructor function and prototype object when using inheritance?

查看:67
本文介绍了使用继承时,构造函数和原型对象之间有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将以下内容视为JavaScript代码段:

Consider the following to JavaScript snippets:

function foo() {
  this.bar = function() { };
}

// or... (if we used an empty constructor function)

foo.prototype.bar = function() { };

我这样做有什么区别?

function baz() {
}

baz.prototype = new foo();

在两种情况下, baz 最终都具有成员 bar ,但是有什么区别?我为什么要在不同的地方这样做?

In both cases baz ends up having a member bar but what's the difference? Why would I do this in different places?

推荐答案

区别在于属性在原型链中的位置.

The difference is where in the prototype chain the property is located.

假设我们有 f = new foo(); b = new baz().那么我们有以下几种情况:

Assuming we have f = new foo(); and b = new baz(). Then we have the following situations:

foo 的定义而不使用原型:

Definition of foo without using a prototype:

+-----------+     +---------------+
|     f     |     | foo.prototype |
| __proto__-+---->| constructor   |  
| bar       |     |               |
+-----------+     +---------------+

bar 是对象本身的属性( f.howOwnProperty('bar')返回 true ).

bar is a property of the object itself (f.howOwnProperty('bar') returns true).

如果您将属性分配给原型,则情况为:

If you assign the property to the prototype instead, the situation is:

+-----------+     +---------------+
|     f     |     | foo.prototype |
| __proto__-+---->| constructor   |  
|           |     | bar           |
+-----------+     +---------------+

f 没有自己的属性 bar ,但该属性与所有其他 foo 实例共享

f does not have its own property bar, but the property is shared with all other foo instances.

与第二个代码段相似,结果为

Similar for the second snippet, which results either in

+-----------+     +---------------+     +---------------+
|     b     |     | foo instance  |     | foo.prototype |
| __proto__-+---->| __proto__    -+---->| constructor   |  
|           |     | bar           |     |               |
+-----------+     +---------------+     +---------------+

+-----------+     +---------------+     +---------------+
|     b     |     | foo instance  |     | foo.prototype |
| __proto__-+---->| __proto__    -+---->| constructor   |  
|           |     |               |     | bar           |
+-----------+     +---------------+     +---------------+


您为什么要这样做?

主要是关于结构而不是浪费内存.您可以在构造函数中将函数添加到对象:

It's mainly about structure and not wasting memory. You can add functions to an object in a constructor function:

function Foo() {
    this.bar = function() {};
}

但这也意味着 Foo 的每个实例都有其自己的功能,即 f1.bar === f2.bar false ,尽管两个函数都做完全相同的事情.

but this also means that each instance of Foo has it's own function, that is, f1.bar === f2.bar is false, although both functions are doing the exact same thing.

使用原型为您提供了一种清晰的方法来分离所有实例和特定于实例的通用属性.

Using the prototype gives you a clean way to separate properties common to all instances and instance-specific ones.

最后,公正" 继承是软件开发(如聚合)中的一个概念,可以在任何有意义的地方使用.您的第二个片段基本上意味着 baz is-a foo ,因此是 baz 实例,自己的属性,具有与 foo 实例(继承)相同的属性.

In the end, it is "just" inheritance which is one concept in software development (like aggregation) and can be used wherever it makes sense. Your second snippet basically means that a baz is-a foo, so a baz instance, in addition to its own properties, has the same properties as a foo instance (inherited).

这篇关于使用继承时,构造函数和原型对象之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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