Hi FE !
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
  • B-005

B-005

for(var index=0;index<5;index++){
  (function(){
    setTimeout(function(){
      console.log(index)
    },index*1000)
  })(index)
}

输出 //5个5 //for循环没有块级作用域 循环结束index为5 setTimeout为异步操作等循环结束后再执行

for(var index=0;index<5;index++){
  (function(index){
    setTimeout(function(){
      console.log(index)
    },index*1000)
  })(index)
}

输出 0 1 2 3 4

Edit this page
最近更新: 2025/6/27 02:24
Contributors: qdleader
qdleader
本站总访问量 129823次 | 本站访客数 12人