箭头函数和普通函数this的区别

157 阅读1分钟

箭头函数

箭头函数的this不会绑定到调用者上

var person = {
    name: "Jason"
};

person.shout = () => console.log(this.name);

person.shout();

运行结果:undefined

person.shout = function(){
    console.log(this.name);
};

运行结果:Jason

下面的三例是箭头函数的等效情况:

person.shout = shout.bind(this);

运行结果:undefined

person.shout.call(this);

运行结果:undefined

person.shout.apply(this);

运行结果:undefined