Twlig / issuesBlog

MIT License
3 stars 0 forks source link

箭头函数和普通函数的区别 #47

Open Twlig opened 2 years ago

Twlig commented 2 years ago

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

(1)箭头函数语法上比普通函数更加简洁

(2)箭头函数没有自己的this,它里面出现this是继承函数所处上下文中的this(使用call,apply等任何方式都无法改变this的指向)

(3)箭头函数中没有arguments(类数组),只能基于…ARG获取传递的参数集合

(4)箭头函数不能被new函数(因为:箭头函数没有this,也没有prototype)

注:回调函数的this一般指向window

function Person(){
    (()=> {console.log(this)})();
}
Person(); //window 
new Person();//Person{}

讨论见:#50