0
var tempOut = false;
var foo = function() {
this.tempIn = false;
this.counter = function() {
setTimeout(function() {
this.tempIn = true;
tempOut = true;
},5000);
};
};
var myFunction = new foo();
myFunction.counter();
console.log(tempOut+ " " + myFunction.tempIn);
嘿那里,我有一个简单的代码,5秒后更改变量。 有2个变量:一个全局(tempOut)和一个本地(tempIn)。当我创建对象从功能foo,并启动计数器函数5秒后,两个变量都应设置为true,但只有tempOut更改。我做错了什么?JavaScript改变对象内部的变量
'this',计时器内,指向'window'对象。 –
加入了我的帖子。 – garryp
非常感谢。我非常感谢你的帮助。 – Humberd