函数内部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
posted @ 2019-08-08 20:31  Diamond蚊子  阅读(1201)  评论(0编辑  收藏  举报