Javascript 什么';这两种继承策略的区别是什么?
我认为它们是等效的,但我不确定:Javascript 什么';这两种继承策略的区别是什么?,javascript,inheritance,prototype,Javascript,Inheritance,Prototype,我认为它们是等效的,但我不确定: var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype =
var __extends = function(child, parent) {
for (var key in parent) {
if (__hasProp.call(parent, key)) child[key] = parent[key];
}
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
及
这两个函数都使用
父对象的所有属性扩展了子对象
(函数)对象,并设置了\uuuuuuuuuuuuuu
属性。然后差异开始了:
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor;
此代码为继承自父.prototype的子.prototype创建原型对象。这是一个什么做的例子。这是经典的JavaScript继承模式
child.prototype = parent.prototype;
child.prototype.constructor = child;
child.__super__ = parent.prototype;
这个代码是垃圾。它将子对象的prototype对象设置为父.prototype
,但在下一行中忘记了现在两个属性都指向同一个对象(子.prototype==父.prototype
)。因此,parent.prototype.constructor===child
和child.\uuuu super\uu===child.prototype
-urgh.这两个函数都使用父对象的所有属性扩展子对象(函数),并设置\uu super\ucode>属性。然后差异开始了:
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor;
此代码为继承自父.prototype的子.prototype创建原型对象。这是一个什么做的例子。这是经典的JavaScript继承模式
child.prototype = parent.prototype;
child.prototype.constructor = child;
child.__super__ = parent.prototype;
这个代码是垃圾。它将子对象的prototype对象设置为父.prototype
,但在下一行中忘记了现在两个属性都指向同一个对象(子.prototype==父.prototype
)。因此,parent.prototype.constructor==child
和child.\uuuu super\uuu==child.prototype
-urgh.传入两个函数???传入两个函数???