預編譯過程
阿新 • • 發佈:2020-12-19
技術標籤:javascript
*/ // 預編譯發生在執行之前 // 1.初始化全域性物件 // Global Object 全域性物件 // 2.全域性作用域下 函式宣告提升 /* Global Object {} function foo: undefined // 3.全域性作用下 變數宣告提升 /* Global Object { function foo: undefined, a: undefined, b: undefined, c: undefined, } */ // 4.給提升的函式賦值 function /* Global Object { function foo: function, a: undefined, b: undefined, c: undefined, } */ // --- Go 預編譯結束 --- // --- 解析執行 --- // 給Global Object 物件下屬性 重新賦值的過程 // var a = '123'; // 執行完畢後go 什麼變化? /* Global Object { function foo: function, a: '123', b: undefined, c: undefined, } */ // var b = 'xk'; /* Global Object { function foo: function, a: '123', b: 'xk', c: undefined, } */ // var c = function(){}; // 宣告變數c 賦值為函式指標 /* Global Object { function foo: function, a: '123', b: 'xk', c: function, } */ // ---Go 這段 重新賦值結束 // --- 函式定義階段 沒執行 // function foo(a,b){ // 函式執行前準備 // 1.初始化Active Object (AO) 物件 /* Active Object{} */ // 2.提升形參 /* Active Object{ a:undefined b:undefined } */ // 3.提升函式 /* Active Object{ a:undefined b:undefined bar:undefined } */ // 4.提升變數 /* Active Object{ a:undefined b:undefined bar:undefined lt:undefined } */ // 5.給提升的函式賦值 function /* Active Object{ a:undefined b:undefined bar:function lt:undefined } */ // 6.形參實參相統一 /* Active Object{ a:1 b:2 bar:ufunction lt:undefined } */ // --- AO預編譯結束 --- // --- 解析執行 --- // var lt = 'lt'; // /* // Active Object{ // a:1 // b:2 // bar:function // lt:'lt' undedined--->'lt' // } // */ // function bar(){}1 // } // 執行完畢 AO 銷燬 // foo(1,2) // var x = 20; // var a = { // x: 15, // fn: function () { // var x = 30; // console.log(this); // return function () { // return this.x // } // } // } // // console.log(a.fn(); // // // a.fn(); // // function f1() { // // return this.x // // } // // console.log(k); // // (a.fn())(); // console.log((a.fn())()); // // // console.log(a.fn()()); // // console.log(a.fn()() == (a.fn())()); // // function f2(){}; // // f1() // function test() { // console.log(this === global); // true // } // test() // // console.log(window); // const obj = 1 // function test() { // console.log(this); // } // const testObj = test.bind(obj); // // test(); // // // testObj(); // // testObj();