1. 程式人生 > 實用技巧 >從原始碼分析ContextLoaderListener的載入過程(帶時序圖)

從原始碼分析ContextLoaderListener的載入過程(帶時序圖)

1.全域性環境下 的this 指向 window



2. 函式的獨立呼叫,函式的內部的this也指向了window

function fn(){

  console.log(this);

}

fn();


3. 當被巢狀的函式獨立呼叫時候,this 預設指向了window
var obj = {

  a:2,

  foo :function (){
    //函式當做物件的方法來呼叫 this指向了obj
    var that = this;     
function test(){         console.log(this);         
         console.log(that.a)



    }   
  test();
  } }
obj.foo();



4. IIFE 自執行函式
內部this指向window
var a = 10;

function foo(){
  console.log(this);
  (function test(that){
    console.log(that.a);
    console.log(this);

  })(this);
}
var obj = {
  a:2,
  foo:foo
}
obj.foo();



5. 閉包 this 指向 window

var a = 0;

var obj = {
  a:2,

  foo:function(){

    var c = this.a;

    return function test(){

      console.log(this);
      return c;
    }
  }
}
var fn = obj.foo();
console.log(fn());