我有一个关于JS OOP Singleton的问题。 我很好奇当这个改变他的参考以及如何使它适应我的例子时,其他方法如何处理。
var myObj = {
test: 12,
testing: [123, 123213],
Modules: {}
};
myObj.Modules.module1 = {
inputs: $('input.elements'),
colors: [],
method1: function (args) {
// doing something
console.log(this.inputs);
},
method2: function (args) {
// doing something
console.log(this.colors);
},
init: function () {
this.inputs.externalMethod({
change: function (event, ui) {
// here is the issue - this refers to change() and not to myObj.
this.method1(1);
},
clear: function () {
// here is the issue - this refers to clear() and not to myObj.
this.method2(2);
}
});
}
};