Javascript mootools/class/objectinstance不';行不通
我不知道为什么我调用a时得到的是“b”而不是“a”。get()。有人能帮我吗Javascript mootools/class/objectinstance不';行不通,javascript,mootools,Javascript,Mootools,我不知道为什么我调用a时得到的是“b”而不是“a”。get()。有人能帮我吗 var tclass = new Class({ initialize:function(n){ this.options = Object.extend({'name' : n}); }, get:function(){ return this.options.name; } }); a = new tclass('a'); b = new tclass('b'); a.get() // b 您
var tclass = new Class({
initialize:function(n){
this.options = Object.extend({'name' : n});
},
get:function(){
return this.options.name;
}
});
a = new tclass('a');
b = new tclass('b');
a.get() // b
您不应该使用新的操作员行3吗
this.options = (new Object()).extend({'name' : n});
您应该将Options类用作mixin,并使用setOptions来正确进行合并:
var tclass = new Class({
Implements: [Options],
initialize: function(n) {
this.setOptions(n);
},
get: function() {
return this.options.name;
}
});
var a = new tclass({
name: 'a'
});
var b = new tclass({
name: 'b'
});
alert(a.get()); // a
话虽如此,在您的示例中,我得到了一个不同的响应—正如预期的—但通过$merge和not extend(merge不取消链接):
但这有什么意义,因为您没有存在的this.options(您需要传递一个对象来扩展它/将它与另一个对象合并)。你不妨这样做:
this.options = {
name: n
};
你真的想要这个.setOptions
p、 美国对于mootools 1.11的实施方式有所不同:
var tclass = new Class({
initialize: function(n) {
this.setOptions(n);
},
get: function() {
return this.options.name;
}
});
tclass.implement(new Options);
var a = new tclass({
name: 'a'
});
var b = new tclass({
name: 'b'
});
alert(a.get()); // a
不,这不对。他正在使用mootools 1.3API调用对象原型Yes baby,就是它(mootools 1.11)!我因树木而错过了树林。哦,天哪。我现在就走。通过选项MIXIN和MOOToE 1.11(从)HEH检查修正的例子,几乎没有,MOOToTS每周都会有3-4个问题,很难节省5分钟,并且回答(如果你想知道答案,不管怎样),是吗?我发现不得不考虑别人的问题是一个学习的好方法。