我不明白,为什么当我设置一个函数的对象实例,每当它从异步访问,如setTimeout或承诺,它是未定义的。有人可以解释吗?有没有解决方法?从异步函数访问未定义的函数
感谢
function Animal() {
this.check = function() {
writeln(typeof this.test);
}
setTimeout(this.check, 1000); // returns undefined
}
var animal = new Animal();
animal.test = function() {
}
animal.check(); // returns function
你的问题可以简化为:'function A(){this.x = 42; this.foo = function(){console.log(this.x); }; } var obj = new A(); var f = obj.foo; f();' – melpomene