javascript原型链:Rectangle.prototype = new Shape()或Rectangle.prototype = Shape? [英] javascript prototype chain: Rectangle.prototype = new Shape() or Rectangle.prototype = Shape?

查看:214
本文介绍了javascript原型链:Rectangle.prototype = new Shape()或Rectangle.prototype = Shape?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了两种不同的模式和解释. DailyJS和其他许多人中的一位: Rectangle.prototype = new Shape();

I saw two different patterns and explanations. One from DailyJS and many others: Rectangle.prototype = new Shape();

,然后是Crockford的此处,这意味着 Rectangle.prototype = Shape;

and then there's Crockford's here which implies just Rectangle.prototype = Shape;

从理论上讲,为什么您需要运行新"软件?它运行构造函数,是的.它还将Rectangle的原型分配给Shape的原型.但是只要将父对象简单分配到原型中,我们就应该能够进行继承.

Now theory-wise, why you need to run the 'new'? it runs the constructor function, yes. it also assigns the Rectangle's prototype the Shape's prototype. but we should be able to do inheritance just with a simple assignment of the parent into the prototype.

我想知道原因是否是原型链.看来,在情况1中,它将创建一个原型.意思是,Rectangle原型将具有Shape prototype . 在第二种情况下,Rectangle的原型将仅具有Shape的方法-而没有Shape的 prototype 方法.

I wondered if the reason is prototype chaining. it seems, that in case 1, it will create a prototype chain. Meaning, that the Rectangle prototype will have the Shape prototype. In the second case, the Rectangle's prototype will just have Shape's methods - but not Shape's prototype methods.

是吗?非常感谢您的任何想法.

Is that right? many thanks for any thoughts.

推荐答案

Crockford 表示仅Rectangle.prototype = Shape;

我真的看不到.

从理论上讲,为什么您需要运行新"软件?它运行构造函数,是的.

Now theory-wise, why you need to run the 'new'? it runs the constructor function, yes.

但是我们实际上不需要(想要),我们只需要从Shape.prototype

Yet we actually don't need (want) that, we only need the inheritance from Shape.prototype

它还将Rectangle的原型分配给Shape的原型.

it also assigns the Rectangle's prototype the Shape's prototype.

并非如此. new Shape创建一个从Shape.prototype继承的新对象,这对我们很重要.你说得对.

Not really. new Shape creates a new object which inherits from Shape.prototype, that's what matters us. You've got that right.

在第二种情况下,矩形的原型将仅具有Shape的方法,而没有Shape的原型方法.

In the second case, the Rectangle's prototype will just have Shape's methods - but not Shape's prototype methods.

是的,这是正确的.您应该从Shape.prototype继承-而是通过Object.create(Shape.prototype)继承,而不是通过创建实例.请参见 JavaScript继承:Object.create与New .这就是Crockford的实际做法-甚至在EcmaScript 5.1引入Object.create之前,他就找到了一种巧妙的方法来使用其助手功能

Yes, that's correct. You should inherit from Shape.prototype - but via Object.create(Shape.prototype), not by creating an instance. See JavaScript inheritance: Object.create vs new or What is the reason [not] to use the 'new' keyword here?. This is what Crockford actually does - and even before Object.create was introduced by EcmaScript 5.1, he found a clever way to do that with his helper function

function object(o) {
    function F() {}
    F.prototype = o;
    return new F();
}

现在是Object.create的常见填充器,在不支持它的浏览器中.

which is now the common shim for Object.create in browsers that don't support it natively.

这篇关于javascript原型链:Rectangle.prototype = new Shape()或Rectangle.prototype = Shape?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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