JS March 15, 2020

你不知道的箭头函数

Words count 3.9k Reading time 4 mins. Read count 0

简言

ES6的箭头函数,我们会在工作中经常知道,但是你知道什么时候使用箭头函数,什么时候不能使用箭头函数吗?

什么时候使用箭头函数?

为啥大多数情况都使用箭头函数?

作用域安全性:当箭头函数被一致使用时,所有东西都保证使用与根对象相同的this。如果一个标准函数回调与一堆箭头函数混合在一起,那么作用域就有可能变得混乱。

紧凑性:箭头函数更容易读写。

清晰度:使用箭头函数可明确知道当前 this 指向

其它情况使用箭头函数。

什么时候不能使用箭头函数?

定义对象方法

定义字面量的方法

const calculator = {
    array: [1, 2, 3],
    sum: () => {
        console.log(this === window); // => true
        return this.array.reduce((result, item) => result + item);
    }
};

console.log(this === window); // => true

// Throws "TypeError: Cannot read property 'reduce' of undefined"
calculator.sum();

calculator.sum 使用箭头函数来定义,但是调用的时候会抛出 TypeError,因为运行时 this.array 是未定义的,调用 calculator.sum 的时候,执行上下文里面的 this 仍然指向的是 window,原因是箭头函数把函数上下文绑定到了 window 上,this.array 等价于 window.array,显然后者是未定义的。

解决的办法是,使用函数表达式或者方法简写(ES6 中已经支持)来定义方法,这样能确保 this 是在运行时是由包含它的上下文决定的,

const calculator = {
    array: [1, 2, 3],
    sum() {
        console.log(this === calculator); // => true
        return this.array.reduce((result, item) => result + item);
    }
};
calculator.sum(); // => 6

定义原型方法

同样的规则适用于原型方法(prototype method)的定义,使用箭头函数会导致运行时的执行上下文错误

function Cat(name) {
    this.name = name;
}

Cat.prototype.sayCatName = () => {
    console.log(this === window); // => true
    return this.name;
};

const cat = new Cat('Mew');
cat.sayCatName(); // => undefined

解决方法:使用传统的函数表达式就能解决问题

定义事件回调函数

const button = document.getElementById('myButton');
button.addEventListener('click', () => {
    console.log(this === window); // => true
    this.innerHTML = 'Clicked button';
});

在全局上下文下定义的箭头函数执行时 this 会指向 window,当单击事件发生时,浏览器会尝试用 button 作为上下文来执行事件回调函数,但是箭头函数预定义的上下文是不能被修改的,这样 this.innerHTML 就等价于 window.innerHTML,而后者是没有任何意义的。

解决方法:使用传统的函数表达式就能解决问题

定义构造函数

构造函数中的 this 指向新创建的对象,当执行 new Car() 的时候,构造函数 Car 的上下文就是新创建的对象,也就是说 this instanceof Car === true。显然,箭头函数是不能用来做构造函数, 实际上 JS 会禁止你这么做,如果你这么做了,它就会抛出异常。

const Message = (text) => {
    this.text = text;
};
// Throws "TypeError: Message is not a constructor"
const helloMessage = new Message('Hello World!');

追求过短的代码

在代码可读性方面,最短的代码可能并不总是最好的。一定程度上,压缩了太多逻辑的简短代码,阅读起来就没有那么直观,

const multiply = (a, b) => b === undefined ? b => a * b : a * b;
const double = multiply(2);
double(3);      // => 6
multiply(2, 3); // => 6

multiply 函数会返回两个数字的乘积或者返回一个可以继续调用的固定了一个参数的函数。代码看起来很简短,但大多数人第一眼看上去可能无法立即搞清楚它干了什么,怎么让这段代码可读性更高呢?有很多办法,可以在箭头函数中加上括号、条件判断、返回语句,或者

function multiply(a, b) {
    if (b === undefined) {
        return function (b) {
            return a * b;
        }
    }
    return a * b;
}

const double = multiply(2);
double(3); // => 6
multiply(2, 3); // => 6
0%