PHP菜鸟学习历程-6

1 闭包创建数组

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4       <meta charset="UTF-8">
 5       <title>17-闭包-数组</title>
 6       <script type="text/javascript">
 7         var num=new Array();
 8 
 9         for(var i=0; i<=5; i++){
10             num[i]=function(){
11                   console.log(i);
12             }
13         }
14         i=100;
15         //以上代码从环境上看连个环境,window和function内部环境
16         //function内部环境访问外部变量信息(实际6个元素访问了6次)
17         //window环境的i变量只有一个,并且for循环执行完毕值为6
18         //因此每个数字元素访问到的结果都是6
19         //console.log(i);  6
20         /*
21         num[0]=function(){console.log(0)}
22         num[1]=function(){console.log(1)}
23         num[2]=function(){console.log(2)}
24         ......
25         num[5]=function(){console.log(5)}
26         */
27 
28         num[4]();  //[6]  4
29         num[1]();  //[6]  1
30         num[3]();  //[6]  3
31         num[5]();
32 
33         //num[6]();
34         //num[10]();
35       </script>
36 </head>
37 <body>
38       
39 </body>
40 </html>
17-闭包-数组

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案