局部变量:
函数内声明的变量、函数参数是局部变量
函数嵌套:
每个函数都有自己作用域,会出现局部作用域嵌套情况
1 var test = "publice"; //全局变量 2 function fun1(){ 3 var test = "private1"; //局部变量 4 function fun2(){ 5 var test = "private2"; //嵌套作用域内的局部变量 6 }; 7 };
优先级别:
在函数体内,局部变量高于全局变量,换句话说,在函数体内局部变量会覆盖全局变量
声明提前:
JavaScript没有块级作用域:JavaScript的函数作用域指在函数内声明的所有变量在函数内始终是可以见的
函数内声明的所有变量,进行编译时,都被提前至函数体顶部,示例:
1 <script type="text/javascript"> 2 var a = 10; 3 console.log(a);//输出10 4 function ss() { 5 console.log(a);//输出undefined 6 var a = 1; 7 console.log(a);//输出1 8 }; 9 dojo.ready(ss); 10 </script>
等价于:
1 <script type="text/javascript"> 2 var a = 10; 3 console.log(a);//输出10 4 function ss() { 5 var a; 6 console.log(a);//输出undefined 7 a = 1; 8 console.log(a);//输出1 9 }; 10 dojo.ready(ss); 11 </script>