跳至主要內容

this 的指向

Mr.Hope...大约 4 分钟JavaScript

在一个对象中绑定函数,称为这个对象的方法。

在 JavaScript 中,对象的定义是这样的:

const xiaoming = {
  name: "小明",
  birth: 1990,
};

但是,如果我们给 xiaoming 绑定一个函数,就可以做更多的事情。比如,写个 age() 方法,返回 xiaoming 的年龄:

const xiaoming = {
  name: "小明",
  birth: 1990,
  age() {
    const y = new Date().getFullYear();
    return y - this.birth;
  },
};

xiaoming.age; // function xiaoming.age()
xiaoming.age(); // 今年调用是25,明年调用就变成26了

提示

const xiaoming = {
  age() {
    return new Date().getFullYear() - this.birth;
  },
};

const xiaoming = {
  age: function () {
    return new Date().getFullYear() - this.birth;
  },
};

的简写方式。

绑定到对象上的函数称为方法,和普通函数也没啥区别,但是它在内部使用了一个 this 关键字,这个东东是什么?

在一个方法内部,this 是一个特殊变量,它始终指向当前对象,也就是 xiaoming 这个变量。所以,this.birth 可以拿到 xiaomingbirth 属性。

让我们拆开写:

function getAge() {
  return new Date().getFullYear() - this.birth;
}

const xiaoming = {
  name: "小明",
  birth: 1990,
  age: getAge,
};

xiaoming.age(); // 25, 正常结果
getAge(); // NaN

单独调用函数 getAge() 怎么返回了 NaN? 请注意,我们已经进入到了 JavaScript 的一个大坑里。

JavaScript 的函数内部如果调用了 this,那么这个 this 到底指向谁?

答案是,视情况而定!

如果以对象的方法形式调用,比如 xiaoming.age(),该函数的 this 指向被调用的对象,也就是 xiaoming,这是符合我们预期的。

如果单独调用函数,比如 getAge(),此时,该函数的 this 指向全局对象,也就是 window

坑爹啊!

更坑爹的是,如果这么写:

const fn = xiaoming.age; // 先拿到 xiaoming 的 page 函数

fn(); // NaN

也是不行的! 要保证 this 指向正确,必须用 obj.xxx() 的形式调用!

由于这是一个巨大的设计错误,要想纠正可没那么简单。ECMA 决定,在 strict 模式下让函数的 this 指向 undefined,因此,在 strict 模式下,您会得到一个错误:

"use strict";

const xiaoming = {
  name: "小明",
  birth: 1990,
  age() {
    return new Date().getFullYear() - this.birth;
  },
};
const fn = xiaoming.age;

fn(); // Uncaught TypeError: Cannot read property 'birth' of undefined

这个决定只是让错误及时暴露出来,并没有解决 this 应该指向的正确位置。

有些时候,喜欢重构的您把方法重构了一下:

"use strict";

const xiaoming = {
  name: "小明",
  birth: 1990,
  age() {
    function getAgeFromBirth() {
      return new Date().getFullYear() - this.birth;
    }

    return getAgeFromBirth();
  },
};

xiaoming.age(); // Uncaught TypeError: Cannot read property 'birth' of undefined

结果又报错了! 原因是 this 指针只在 age 方法的函数内指向 xiaoming,在函数内部定义的函数,this 又指向 undefined 了! (在非 strict 模式下,它重新指向全局对象 window! )

修复的办法也不是没有,我们用一个 that 变量首先捕获 this:

"use strict";

const xiaoming = {
  name: "小明",
  birth: 1990,
  age() {
    const that = this; // 在方法内部一开始就捕获 this

    function getAgeFromBirth() {
      // 用that而不是this
      return new Date().getFullYear() - that.birth;
    }

    return getAgeFromBirth();
  },
};

xiaoming.age(); // 25

const that = this;,您就可以放心地在方法内部定义其他函数,而不是把所有语句都堆到一个方法中。

apply

虽然在一个独立的函数调用中,根据是否是 strict 模式,this 指向 undefinedwindow,不过,我们还是可以控制 this 的指向的!

要指定函数的 this 指向哪个对象,可以用函数本身的 apply 方法,它接收两个参数,第一个参数就是需要绑定的 this 变量,第二个参数是 Array,表示函数本身的参数。

apply 修复 getAge() 调用:

function getAge() {
  return new Date().getFullYear() - this.birth;
}

const xiaoming = {
  name: "小明",
  birth: 1990,
  age: getAge,
};

xiaoming.age(); // 25
getAge.apply(xiaoming, []); // 25, this指向xiaoming, 参数为空

另一个与 apply() 类似的方法是 call(),唯一区别是:

apply() 把参数打包成 Array 再传入;

call() 把参数按顺序传入。

比如调用 Math.max(3, 5, 4),分别用 apply()call() 实现如下:

Math.max.apply(null, [3, 5, 4]); // 5
Math.max.call(null, 3, 5, 4); // 5

对普通函数调用,我们通常把 this 绑定为 null。

装饰器

利用 apply(),我们还可以动态改变函数的行为。

JavaScript 的所有对象都是动态的,即使内置的函数,我们也可以重新指向新的函数。

现在假定我们想统计一下代码一共调用了多少次 parseInt(),可以把所有的调用都找出来,然后手动加上 count += 1,不过这样做太傻了。最佳方案是用我们自己的函数替换掉默认的 parseInt():

"use strict";

let count = 0;
const oldParseInt = parseInt; // 保存原函数

window.parseInt = function () {
  count += 1;

  return oldParseInt.apply(null, arguments); // 调用原函数
};

// 测试:
parseInt("10");
parseInt("20");
parseInt("30");
console.log(`count = ${count}`); // 3
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3