前言
ES6中添加了箭头函数,可以更方便绑定this作用域了 O.o
至于使用,我觉得一个实例就够了
实例代码
const x = 1;
global.x = 2;
const obj = {
x: 3,
fun: function () {
console.log(this.x); // 3 this => obj
let fun2 = () => {
console.log(this.x); // 3 this => fun => obj
};
let fun3 = function () {
console.log(this.x); // 2 this => global
};
fun2();
fun3();
}
};
obj.fun();
console.log(this.x); // undefined this => {}