ES6新特性5:函数的扩展之箭头函数
2019-12-24 本文已影响0人
泰然自若_750f
箭头函数在ES6中最常用的特性,学习好至关重要,特别在Vue项目和React项目。
箭头函数:ES6中使用(=>)来定义函数。可以用来:简化函数和不需要绑定 this
特性:
1:函数体内的this对象,是定义时所在的对象,而不是使用时所在的对象。箭头函数中访问的this实际上是其父级作用域中的this,箭头函数本身的this是不存在的,这样就相当于箭头函数的this是在声明的时候就确定了(因为相当于作用域嘛)。见下实例。

箭头函数可以让setTimeout里面的this,绑定定义时所在的作用域,而不是指向运行时所在的作用域。(一般setTimeout this指向 window对象,因为存在延时执行)


2:箭头函数不能作为构造函数

3:不绑定arguments(如果有要使用arguments的时候可以使用rest参数代替)

rest参数代替

4:不可以使用yield命令,因此箭头函数不能用作Generator函数。