这可能非常愚蠢,但它已经困扰我一段时间了。试图用 AngularJS 做点什么,这让我很难受。
在回调中我有这样的东西:
var xyz = {
"modA" : null,
"modB" : null,
"modC" : null
}
然后我附上一些方法...
xyz.setModA = function(elementId) {
var someConfig = {};
// attach some kind of event callback on stateChange
someConfig.events.onStateChange = this.helper;
this.modA = new someObject(someConfig);
}
xyz.setModC = function(vars) {
....
}
xyz.helper = function(event) {
....
this.setModC(vars);
}
当我们实例化的 modA 对象的状态发生变化时,就会发生 onStateChange 事件,并且正确地触发了我拥有的 xyz.helper 方法,但随后我立即得到了一个 Uncaught TypeError: undefined is not a function 吗?出于某种原因,当事件触发时它现在没有看到 setModC 方法?
最佳答案
事件处理程序中的上下文 (this
) 不是所需的对象。
为确保上下文正确,更改
someConfig.events.onStateChange = this.helper;
到
someConfig.events.onStateChange = this.helper.bind(this);
关于javascript - 为什么我不能访问这个 JS 函数? undefined 不是函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23959777/