有人能解釋下繼承的實現之間的區別:JavaScript的原型繼承案件
function Parent(name) {
this.name = name;
this.parentFoo = function() {
//...
}
}
function Child() {
this.additional = true;
Parent.apply(this, arguments); // Is it for copying parent fields to child object?
}
Child.prototype = Object.create(Parent.prototype);
和
function Parent(name) {
this.name = name;
}
Parent.prototype.parentFoo = function() {
//....
}
function Child() {
this.additional = true;
Parent.apply(this, arguments); // Is it for copying parent fields to child object?
}
Child.prototype = Object.create(Parent.prototype);
我的意思是這將是更好的使用,增加功能,直接構造函數或原型?我在問,因爲
var childObj = new Child("Child name");
var parentObj = new Parent("Parent name");
childObj.foo();
parentObj.foo();
在這兩種情況下都能很好地工作。 謝謝!
當然,我已經更新了第二個案例。我從一開始就提到它。 – sidlejinks
因此,由於重新定義和重新綁定每種方法,我們在第一種情況下會出現性能損失,對嗎? – sidlejinks
@sidlejinks我相信如此。 – SimpleJ