函數內部this的指向以及改變this指向


1、函數也是對象

  • 函數的創建==>底層都是new關鍵字創建函數

    • 函數聲明:

      1 function sum(a,b) {
      2     console.log(a + b);
      3 }
    • 函數表達式:

      1 var sum = function sum(a,b) {
      2     console.log(a + b);
      3 }
    • new關鍵字創建函數:

      1 // 語法:var 變量名 = new Function ('形參1','形參2',...,'函數體中的代碼')
      2 var sum = new Function('a','b','console.log(a + b)');
      3 sum(10,20);

       

       

2、函數內部this的指向

this的指向關鍵是看函數的調用方法

2.1 普通函數中this的指向

  • this指向window

  • 代碼:

     1 // 【在普通函數中this指向window】
     2 // function fn() {
     3 //   console.log(this);
     4 // }
     5 // fn();
     6  7 // var fn = function() {
     8 //   console.log(this);
     9 // };
    10 // fn();
    11 12 // (function(){
    13 //   console.log(this);
    14 // })();
    15 16 17 // function fn1() {
    18 //   function fn2() {
    19 //     console.log(this);
    20 //   }
    21 //   fn2();
    22 // }
    23 // fn1();

2.2 構造函數中this的指向

  • this指向當前創建的對象----在內存中開辟的空間

  • 代碼:

     1  // 【在構造函數中this指向當前創建的對象】
     2 function Student(name,age) {
     3     this.name = name;
     4     this.age =age;
     5 }
     6 // 原型中添加方法
     7 Student.prototype.sayHi = function() {
     8     console.log('我叫' + this.name);
     9 };
    10 11 var stu1 = new Student('張三',10);    // ==>this指向stu1
    12 var stu2 = new Student('李四',10);    // ==>this指向stu2

2.3 對象方法中的this指向

  • this指向調用者

  • 代碼:

     1 // 【在方法中,this指向調用者。  對象.方法()】
     2 function Student(name,age) {
     3     this.name = name;
     4     this.age =age;
     5 }
     6 // 原型中添加方法
     7 Student.prototype.sayHi = function() {
     8     console.log('我叫' + this.name);
     9 };
    10 11 var stu1 = new Student('張三',10);    // ==>this指向stu1
    12 stu1.sayHi();   // ==>方法sayHi中的this指向stu1

2.4 定時器中this的指向

  • this指向window

  • 代碼:

    1 // 【在定時器中this指向windnow】
    2 setTimeout(function(){
    3    console.log(this);
    4 },5000);

2.5 事件處理程序中this指向

  • this指向事件源

  • 代碼:

    1 // 【在事件處理程序中,this指向事件源】
    2  document.onclick = function() {
    3     console.log(this);
    4  };

3、改變this的指向

3.1 call方法

  • 語法:函數名.call(調用者,實參1...)

  • 作用:函數被借用時,會立即執行,並且函數體內的this會指向調用者(借用者)

  • 代碼:

    1  function fn(name, age) {
    2    this.name = name;
    3    this.age = age;
    4    console.log(this == obj);    //==>true
    5  }
    6 7  // 對象字面量
    8  var obj = {};
    9  fn.call(obj, '李四', 11);    //==>fn中的this指向obj

3.2 apply方法

  • 語法:函數名.apply(調用者,[實參1,實參2,....]) ==>參數以數組的形式

  • 作用:函數被借用是,會立即執行,並且函數體內的this會指向調用者(借用者)

  • 代碼:

    1 function fn(name, age) {
    2    this.name = name;
    3    this.age = age;
    4    console.log(this == obj);    //==>true
    5  }
    6 7  // 對象字面量
    8  var obj = {};
    9  fn.call(obj, ['李四', 11]);  //==>fn中的this指向obj

3.3 bind方法

  • 語法:函數名.bind(調用者,實參1....)

  • 作用:函數被借用時,不會立即執行,而是返回一個新的函數。需要自己手動調用新的函數。

  • 代碼:

     1 function fn(name, age) {
     2    this.name = name;
     3    this.age = age;
     4    console.log(this == obj);    //==>true
     5  }
     6  7  // 對象字面量
     8  var obj = {};
     9  var newFn = fn.bind(obj, '李四', 11);
    10  newFn();
    11 // fn.bind(obj, '李四', 11)();

3.4 使用

  • 代碼:

     1 //  this指向window
     2 var name = 'window';
     3 var obj = {name = 'obj'};
     4 setTimeout(function(){
     5     console.log(this.name); //==>輸出window
     6 },2000)
     7 //  this指向obj
     8 var name = 'window';
     9 var obj = {name = 'obj'};
    10 setTimeout(function(){
    11     console.log(this.name); //==>輸出obj
    12 }.bind(obj),2000)   //  bind不會立即執行
  • 偽數組借用數組的push方法實現增加

  • 代碼:

     1 var arr = [10,20,30,40];
     2 //  arr.push(50);   //  push為數組中的方法
     3 var obj = {
     4     0:10,
     5     1:20,
     6     2:30,
     7     3:40,
     8     length:4
     9 }
    10 //  console.log(arr instanceof Array);  ==>true
    11 //  console.log(obj instanceof Array);  ==>false
    12 //  Array.prototype.push    ==>構造函數Array的原型中的push方法
    13 Array.prototype.push.call(obj,50);  //  ==>改變push中this指向為obj


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM