JavaScript执行步骤
1.检查通篇的语法错误
2.预编译过程
3.解释一行,执行一行
1 (a) 2 test() 3 function test() { 4 console.log(1) // 1 5 } 6 7 (b) 8 console.log(a); // undefined 9 var a ; 10 11 函数申明整体提升,变量只有申明提升,赋值不提升 12 13 (c) 14 console.log(a) // function a(a){} 15 function a(a){} 16 var a = 1 17 18 (d) 19 var a = 1 20 b = 2 21 console.log(window.b) // a = window.a 22 // b = window.b
暗示全局变量 imply global variable
1 (a) 2 function test(){ 3 var a = b = 1 4 // 1.先声明 var a 5 // 2.1赋值给b 6 // 3.b的值赋值给a 7 } 8 console.log(a) // error 9 console.log(window.a) // undefined 10 console.log(b) // 1 11 console.log(window.b) // 1 12 13 (b) 14 function test(a) { 15 console.log(a) // function a() {} 16 var a = 1 17 console.log(a) // 1 18 function a() {} 19 console.log(a) // 1 20 var b = function () {} 21 console.log(b) // function(){} 22 function d() {} 23 } 24 test(2)
函数预编译:AO activation object (活跃对象,函数上下文)
1 function test(a) { 2 console.log(a) // function a() {} 3 var a = 1 4 console.log(a) // 1 5 function a() {} 6 console.log(a) // 1 7 var b = function () {} 8 console.log(b) // function(){} 9 function d() {} 10 } 11 test(2) 12 13 执行过程 14 15 AO = { 16 17 } 18 19 <-预编译过程-> 20 21 第一步:寻找形参和变量申明 22 AO = { 23 a: undefined, 24 b: undefined 25 } 26 27 第二步:实参赋值给形参 28 AO = { 29 a: undefined -> 2, 30 b: undefined 31 } 32 33 第三步:寻找函数体的申明,并赋值 34 AO = { 35 a: undefined -> 2 -> function a() {}, 36 b: undefined, 37 d: function d() {} 38 } 39 40 <-代码执行过程-> 41 第四步:执行函数体第一句 42 // console.log(a):function a(){} 43 44 第五步:执行函数体第二局 45 //a = 1 46 AO = { 47 a: undefined -> 2 -> function a() {} -> 1, 48 b: undefined, 49 d: function d() {} 50 } 51 52 ....
全局预编译:GO global object 全局上下文
1. 找变量
2. 找函数申明
3. 执行
1 (a) 2 var a = 1 3 function a() { 4 console.log(2) 5 } 6 console.log(a) // 1 7 8 GO = { 9 a: undefined -> function a(){} -> 1 10 } 11 12 其实GO就是window,即GO === window 13 14 (b) 15 console.log(a, b) // function a(){} undefined 16 function a() {} 17 var b = function () {} 18 19 GO = { 20 b: undefined 21 a: function a(){} 22 }
练习
1 练习 2 3 (1) 4 function test() { 5 var a = b = 1; 6 console.log(b); // 1 7 } 8 test(); 9 步骤: 10 <-预编译过程-> 11 1. 12 GO = { 13 14 } 15 16 2. 17 GO = { 18 19 } 20 21 AO = { 22 a: undefined 23 } 24 <-执行过程-> 25 3. 26 GO = { 27 b: 1 28 } 29 30 AO = { 31 a: undefined 32 } 33 34 4. 35 GO = { 36 b: 1 37 } 38 39 AO = { 40 a: undefined -> 1 41 } 42 43 (2) 44 var b = 3; 45 console.log(a); // function a(a){...} 46 function a(a){ 47 console.log(a) // function a(){} 48 var a = 2; 49 console.log(a); // 2 50 function a(){} 51 var b = 5; 52 console.log(b) // 5 53 } 54 a(1); 55 56 步骤: 57 58 <-编译过程-> 59 60 GO = { 61 b: undefined, 62 a: function a(){...} 63 } 64 65 AO = { 66 a: undefined -> 1 -> function a(){} 67 b: undefined 68 } 69 70 <-执行过程-> 71 72 GO = { 73 b: undefined -> 3, 74 a: function a(){...} 75 } 76 77 AO = { 78 a: undefined -> 1 -> function a(){} -> 2 79 b: undefined -> 5 80 } 81 82 83 (3) 84 a = 1; 85 function test(){ 86 console.log(a); // undefined 87 a = 2; 88 console.log(a); // 2 89 var a = 3; 90 console.log(a) // 3 91 } 92 test() 93 94 (4) 95 function test(){ 96 console.log(b); // undefined 97 if (a) { 98 var b = 2; 99 } 100 101 c = 3; 102 console.log(c) // 3 103 } 104 105 var a; 106 test(); 107 a = 1; 108 console.log(a); // 1 109 110 (5) 111 function test(){ 112 return a; 113 a = 1; 114 function a(){} 115 var a = 2; 116 } 117 console.log(test()); // function a(){} 118 119 120 (6) 121 function test(){ 122 a = 1; 123 functin a(){} 124 var a = 2; 125 return a; 126 } 127 console.log(test()); // 2 128 129 (7) 130 a = 1; 131 function test(e){ 132 function e(){} 133 arguments[0] = 2; 134 console.log(e); // 2 135 if(a){ 136 var b = 3; 137 } 138 var c; 139 a = 4; 140 var a; 141 console.log(b); // undefined 142 f = 5; 143 console.log(c); // undefined 144 console.log(a); // 4 145 } 146 var a; 147 test(1);