几个求值js

 1 var a = 1;
 2     function aa(){
 3         a = 2;
 4     }
 5     aa();
 6     console.log("a:", a);         // 2
 7 
 8     var b = 1;
 9     function bb(){
10         b = 2;
11         var b = 3;
12     }
13     bb();
14     console.log("b:", b);        // 1
15 
16     function foo(){
17         var a1 = 1;
18         function fn1(){
19             fn2();
20             var a1 = 3;
21         }
22         function fn2(){
23             a1 = 2;
24         }
25         fn1();
26         return a1;
27     }
28     var c = foo();
29     console.log("c:", c)        // 2
30 
31     function foo2(){
32         var a2 = 1;
33         function fn1(){
34             a2 = 2;
35             var a2 = 3;
36         }
37         
38         fn1();
39         return a2;
40     }
41     var d = foo2();
42     console.log("d:", d)        // 1

注意函数的闭包作用域,变量声明提前,变量不加var 时候是全局变量,及函数执行环境

posted @ 2013-09-27 10:35  楚玉  阅读(166)  评论(0编辑  收藏  举报