es6箭头函数避免使用场景。

113 阅读1分钟

一、

事件回调函数、

ele.addEventListener('click',()=>{

console.log(this); // 当前this指向window  

});

ele.addEventListener('click',function(){

console.log(this); // 当前this指向ele对象

});

二、

原型中使用箭头函数、

function Father(){

this.name = "李四";

}

Father.prototype.sayName = ()=>{

console.log(this.name); // this指向window

}

Father.prototype.sayName2 = function(){

console.log(this.name); // 李四

}

三、

定义对象、

let obj = {

name:"李四",
sayName:()=>{
    console.log(this.name); // this指向window
}

}

let obj1 = { name:"李四", sayName:function(){ console.log(this.name); // 李四 } }