【发布时间】:2016-04-09 20:18:20
【问题描述】:
我是 JS 中 OOP 的新手。现在我正在尝试在实例中调用变量,但是如何在函数中调用它?
var foo = function() {
this.a = 1; // how can I call 'a' in the setTimeout() function
this.start = function(i) {
if (i<3){
window.setTimeout(function() {
console.log(a); // this line shows undefined
console.log(this.a); // this line indicates 'this' is window
i++;
start(i); // this one does not work as well
}, 2000);
}
};
};
var bar = new foo();
bar.start(0);
【问题讨论】:
标签: oop recursion settimeout