Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

匿名函数(自执行函数IIFE)的作用是什么 #24

Open
Liqiuyue9597 opened this issue Aug 10, 2020 · 0 comments
Open

匿名函数(自执行函数IIFE)的作用是什么 #24

Liqiuyue9597 opened this issue Aug 10, 2020 · 0 comments

Comments

@Liqiuyue9597
Copy link
Owner

在 IIFE 中所建立的变量,都不会影响到全局执行上下文所建立的变量。也就是说放在 IIFE 里面的变量,并不会影响到其他外层的变量,也不会被外层的变量影响。

一道经典的面试题就需要IIFE解决。
按照顺序每隔一秒打印数组的元素。

var arr = [1,2,3,4,5]
for(var i=0; i<arr.length; i++){
  (function(index){
    setTimeout(function(){
      console.log(arr[index])
    },1000) 
  })(i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant