1 console.log(a); // undefined 2 var a = 12; // 12 3 function fn() { 4 console.log(a); // undefined 5 var a = 13; // 13 6 } 7 fn(); 8 console.log(a); // 12
1 console.log(a); // undefined 2 var a = 12; // 12 3 function fn() { 4 console.log(a); // 12 5 a=13; // 13 6 } 7 fn(); 8 console.log(a); // 13
9 // 结果为 undefined 、12 、13
1 console.log(a); // a is not defined 2 3 a=12; 4 function fn() { 5 console.log(a); 6 a = 13; 7 } 8 fn(); 9 console.log(a); 10 // a is not defined
1 var foo = 1; 2 function bar () { 3 /** 4 * 形参赋值:无 5 * 变量提高 6 * var foo 7 */ 8 if (!foo) { // ==> !undefined ==> true 9 var foo = 10; 10 } 11 console.log(foo);// foo => foo 12 }
1 var n = 0; 2 function a() { 3 var n = 10; 4 function b() { 5 n++; 6 console.log(n); 7 console.log(this); 8 } 9 b(); 10 return b; 11 } 12 var c = a(); 13 c(); 14 console.log(n); 15 16 // --------------------------------- 17 // 建立全局做用域 n、 c、a(a =>> 指向函数堆栈的地址 ) 18 // 开始执行 n = 0; c = a() 此过程会把 a 执行结果赋值给 c 变量); 19 // 执行 a() 时候,建立私有 window.a 做用域,有变量 n、 b (b =>> 指向函数堆栈的地址 000111) 20 // 开始执行 n = 10; b(); 21 // 执行 b() 时候,建立 window.a.b 做用域,变量为 n 取自上级做用域 a.n; 22 // 执行 n++; console.log(n);( n => 10 + 1; console.log(11);) 23 // 以后 a.b 做用域会销毁,由于此做用域在其余处未被引用 24 // return b; (b => 函数堆栈的地址 000111) 25 // var c = a (); 会把 a 做用域中的 b 值赋给变量 c, 因此 a 做用域不销毁。 26 // 开始执行 c(); 27 // 造成 c 时候,建立私有做用域,有变量 n 取自上级做用域仍是 a.n ; 28 // 执行 n++; console.log(n);( n => 0 + 1; console.log(1);) 29 30 // 函数做用域只和在哪里建立有关,和在哪里执行没有关系。