for(var i =0;i<5;i++){
setTimeout(function () {
console.log(i); //输出5个5 ,不用闭包,也不用let,怎么改改让他输出12345?
},0)
}
for(var i =0;i<5;i++){
setTimeout(function () {
console.log(i); //输出5个5 ,不用闭包,也不用let,怎么改改让他输出12345?
},0)
}
var arrayIteratpr={
values:[0,1,2,3,4],
currentIndex:-1,
next:function(){
this.currentIndex++;
if(this.currentIndex===this.values.length){
return undefined;
}else{
return this.values[this.currentIndex];
}
}
};
var timerLogic=function(){
var value=this.next();
if(Object.prototype.toString.call(value)==="[object Undefined]"){
console.log("done");
}else{
console.log(value);
setTimeout(timerLogic,0)
}
}.bind(arrayIteratpr);
setTimeout(timerLogic,0);
10 回答11.2k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.8k 阅读✓ 已解决
3 回答2.3k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
把var 改成 let
或者
再或者
但也不算完全不是闭包吧