我有一个JavaScript类JavaScript类 - this.method调用的setInterval
function MyClass() {
MyClass.prototype.fooMethod = function() {
// some logic goes here...
}
MyClass.prototype.fooMethod2 = function() {
this.fooMethod();
}
}
一切正常,当我打电话是这样的:
var m = MyClass();
m.fooMethod2();
然而,当我有这样的代码被称为的setInterval我得到一个错误: “遗漏的类型错误:对象[对象DOMWindow]有没有方法 'fooMethod'”
var m = MyClass();
var intervalId = setInterval(m.fooMethod2, 100);
我有没有什么办法可以使用setInverval来调用我的方法?
@Alex韦恩是正确的,但要帮助启发你,为什么,你可能需要阅读http://stackoverflow.com/questions/133973/how-does-this-keyword-work-within- a-javascript-object-literal – Matt 2012-04-12 18:20:00