JavaScript基础-var
使用var 的变量会在js开头先申明,故此var变量申明顺序不影响,
1 alert("aa" in window); // false 2 alert("bb" in window); // true 3 aa=5; 4 var bb=2;
变量使用var(显式申明)为局部变量,变量不使用var(隐式申明)为全局变量,
1 function test(){ 2 a = "12"; 3 var b = "eaa"; 4 } 5 test(); 6 alert(a); // 12 7 alert(b); // b is not defined
隐式申明 创建了一个全局对象的属性,可以删除,
1 var a = 5; 2 b=4; 3 delete a; 4 delete b; 5 alert(a); // 5 6 alert(b); // b is not defined
var a=1; 申明的是本地变量a并赋值1;
b=2; 先寻找是否有变量b申明,有则使用该变量,没有则申明一个全局变量
1 var num1 = 1; 2 num2 = 2; 3 console.log("1--"+num1+","+num2); // 1,2 4 +function(){ 5 num1 = 11; 6 var num2 = 22; 7 console.log("2--"+num1+","+num2); // 11,22 8 (function(){ 9 console.log("3--"+num1+","+num2); // undefined,22 10 var num1 = 111; 11 num2 = 222; 12 num3 = 333; 13 console.log("4--"+num1+","+num2+","+num3); // 111,222,333 14 }()) 15 console.log("5--"+num1+","+num2+","+num3); // 11,222,333 16 }(); 17 console.log("6--"+num1+","+num2+","+num3); // 11,2,333