1、箭头函数体中的 this 对象,是定义函数时的对象,而不是使用函数时的对象。
function fn(){setTimeout(()=>{// 定义时,this 绑定的是 fn 中的 this 对象
console.log(this.a);},0)}
var a =20;// fn 的 this 对象为 {a: 18}
fn.call({a:18});// 18
2、没有 this、super、arguments 和 new.target 绑定。
3、不可以作为构造函数,也就是不能使用 new 命令,否则会报错
箭头函数适合使用场景
ES6 之前,JavaScript 的 this 对象一直很令人头大,回调函数,经常看到 var that = this 这样的代码,为了将外部 this 传递到回调函数中,那么有了箭头函数,就不需要这样做了,直接使用 this 就行。
// 回调函数
var Person ={'age':18,'sayHello':function(){setTimeout(function(){
console.log(this.age);});}};
var age =20;
Person.sayHello();// 20
var Person1 ={'age':18,'sayHello':function(){setTimeout(()=>{
console.log(this.age);});}};
var age =20;
Person1.sayHello();// 18