JS基础_函数作用域练习

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         <script type="text/javascript">
 7         
 8             var a = 123;
 9             function fun(){
10                 alert(a);//undefined
11                 var a = 456;
12                 alert(a);//456
13             }
14             fun();
15             alert(a);//123
16             
17             //---------------------------------------------------
18             
19             var b = 123;
20             function fun(){
21                 alert("b = "+b);//123
22                 b = 456;
23             }
24             fun();
25             alert("b = "+b);//456,全局的b变成了456
26             
27             //---------------------------------------------------
28             
29             var c = 123;  
30             function fun(c){
31                 alert("c = "+c);//undefined
32                 c = 456;//对外面的c没影响
33             }
34             fun();
35             alert("c = "+c);//123
36             
37             //---------------------------------------------------
38             
39             var d = 123;  
40             function fun(d){
41                 alert("d1 = "+d);//789 
42                 d = 456;
43             }
44             fun(789);
45             alert("d = "+d);//123
46             
47             
48         </script>
49     </head>
50     <body>
51     </body>
52 </html>

 

posted @ 2017-10-19 21:35  星辰郎周  阅读(554)  评论(0编辑  收藏  举报