1 function asyncFunction(callback){
 2     setTimeout(function(){
 3         callback()
 4     },200);
 5 }
 6 
 7 var color = 'blue';
 8 //调用上面的函数
 9 asyncFunction(function(){
10     console.log('the color is'+color);                             //green
11 });
12 //闭包函数
13 //To "freeze" the contents of the color variable you can modify your logic and use a JavaScript closure.
14 (function(color){
15     asyncFunction(function(){
16         console.log('the color is'+color);                        //blue
17     });
18     
19 })(color);
20 
21 color = 'green';

1.By making "color" an argument for anonymous function, it becomes local to the scope of that function and

when the value of color is changed outside of the anonymous function,the local version is unaffected.

相关文章:

  • 2021-06-11
  • 2021-09-01
  • 2021-05-20
  • 2021-06-11
  • 2021-08-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2021-07-03
  • 2021-10-09
相关资源
相似解决方案