我不確定使用OLOO繼承鏈中的每個對象都是獨(dú)立的對象屬性的最佳方法.
檢查此小提琴或考慮以下代碼: http:///HB7LU/19413/
Parent = {
array: [],
add: function(element) {
this.array.push(element this.array.length.toString());
return this;
},
getAll: function() {
return this.array;
}
};
Child = Object.create(Parent, {
removeAllButOne: { value: function() {
this.array.splice(1);
return this;
}}
});
foo = Object.create(Parent);
foo.add('foo');
bar = Object.create(Child);
bar.add('bar');
在小提琴中,單擊foo或bar文本將調(diào)用foo.add(…)或bar.add(…)函數(shù),將一個元素添加到對象數(shù)組,從而導(dǎo)致一個額外的< p>.標(biāo)簽在輸出中. 結(jié)果不是我想要的. foo和bar共享同一數(shù)組.但是很容易理解會發(fā)生什么,如果我們查詢對象繼承,我們會看到以下內(nèi)容:

好的,那我該怎么辦?我想到了兩種選擇:
選項1) http:///HB7LU/19419/
Parent = function() {
return {
array: [],
add: function(element) {
this.array.push(element this.array.length.toString());
return this;
},
getAll: function() {
return this.array;
}
};
};
Child = Object.create(Parent(), {
removeAllButOne: { value: function() {
this.array.splice(1);
return this;
}}
});
foo = Object.create(Parent());
foo.add('foo');
bar = Object.create(Child);
bar.add('bar');
這將創(chuàng)建一個新的Parent對象,每次創(chuàng)建Parent對象或從(新的)Parent對象“繼承”子對象時,都會創(chuàng)建Parent對象的所有功能.雖然這解決了我遇到的問題,但始終為每個子類型對象重復(fù)創(chuàng)建相同的函數(shù)似乎是一個壞主意.
選項2) http:///HB7LU/19420/
Parent = Object.create({
add: function(element) {
this.array.push(element this.array.length.toString());
return this;
},
getAll: function() {
return this.array;
}
}, {
ctor: { value: function(someArgs) {
this.array = [];
// maybe use someArgs
return this;
}}
});
Child = Object.create(Parent, {
removeAllButOne: { value: function() {
this.array.splice(1);
return this;
}}
});
foo = Object.create(Parent).ctor();
foo.add('foo');
bar = Object.create(Child).ctor();
bar.add('bar');
這似乎也解決了問題,但避免了Parent對象及其功能的重新生成.那么這是走的路嗎?如果我在繼承鏈中有多個孩子也都有私有財產(chǎn)怎么辦?
像這樣嗎
Child = Object.create(Parent, {
ctor: { value: function(someArgs) {
this.__proto__.ctor(someArgs);
this.otherPrivate = {};
// maybe use someArgs
return this;
}},
removeAllButOne: { value: function() {
this.array.splice(1);
return this;
}}
});
孩子會用自己的功能遮蓋父ctor …但是在其ctor函數(shù)中,他們可以調(diào)用父ctor而不破壞功能.
意見和建議深表感謝,謝謝! 解決方法: 最簡單的方法是使用構(gòu)造函數(shù),因此始終在實(shí)例上將數(shù)組創(chuàng)建為自己的屬性
// define Parent
function Parent() {
this.array = []; // array will be an instance property
}
Parent.prototype = {}; // inherit all the goodies from Object.prototype
Object.assign(Parent.prototype, { // using `Object.assign` for shorthand
add: function (element) {
this.array.push(element this.array.length.toString());
return this;
},
getAll: function () {
return this.array;
}
});
// define Child
function Child() {
Parent.apply(this); // apply Parent constructor to the instance
}
Child.prototype = Object.create(Parent.prototype); // inherit Parent's prototype chain
Object.assign(Child.prototype, {
removeAllButOne: function () {
this.array.splice(1);
return this;
}
});
現(xiàn)在有
var a = new Child(),
b = new Child();
a.array === b.array; // false
您也可以使用ES 6的類來編寫此代碼,但這只是我上面編寫的語法糖,將產(chǎn)生相同的結(jié)構(gòu). 來源:https://www./content-1-565651.html
|