:我有一个用“显示模块模式”编写的 javascript 类:
myObject = function () {
var varA,
varB,
methodA = function (data) {
//Some code...
},
methodB = function (data) {
var that = this;
that.methodA("a"); // --> 'that' recognize only public methods: 'methodB' / 'methodC' !!!
},
methodC = function (data) {
};
return {
methodB : methodB,
methodC : methodC,
};
} ();
正如您在“methodB”中的“this”中看到的那样,它不识别类的私有方法。
编辑: 我的意图是从公共类中调用一个辅助私有方法。在这个私人课程中,我需要“这个”。如果我直接从 'methodB' 调用 'methodA("a")'(没有 'that')我没有 'this'('this' 将是全局上下文)。解决方案将是:
methodA.call(this, "a");