我從EASELJS庫繼承對象。 爲了簡化問題,我將代碼縮減爲最小格式。JavaScript對象成員變量不克隆
我有一個類:
this.TESTProg = this.TESTProg || {};
(function() {
var _jsbutton = function(x, y, text, icon) {
p.init(x, y, text, icon);
};
var p = _jsbutton.prototype = new createjs.Container();
p.x = 0;
p.y = 0;
p.text = null;
p.icon = null;
p.init = function(x, y, text, icon) {
this.x = 0 + x;
this.y = 0 + y;
this.text = "" + text;
this.icon = null;
};
TESTProg._jsbutton = _jsbutton;
})();
然後我用它在另一個JS對象:
var buttoncancel = new SJSGame._jsbutton(
profileselConfig.cancelx, //this is defined in another jsfile:
profileselConfig.cancely,
"cancel", "_cancel.png");
console.log(buttoncancel.y); //this gives 240
var buttoncancel2 = new SJSGame._jsbutton(
profileselConfig.cancelx,
profileselConfig.cancely - 40,
"cancel", "_cancel.png");
console.log(buttoncancel.y); //this gives 200
console.log(buttoncancel2.y); //this gives 200
buttoncancel2.y = 100;
console.log(buttoncancel.y); //this now gives 200 (not changed by the second object)
console.log(buttoncancel2.y); //this now gives 100
配置文件:
var _profileselConfig = function(){
this.cancelx = 0;
this.cancely = 240;
};
profileselConfig = new _profileselConfig();
什麼我做錯了什麼?
我已經使用0 +來避免通過引用,它不工作。我現在應該怎麼做?有什麼建議麼?謝謝。
也許檢查[EaselJS:有人可以解釋演示中使用的繼承模式?](http://stackoverflow.com/questions/18008421/easeljs-can-somebody-explain-the-inheritance-pattern-used-in -demos) – Bergi