箭头函数和普通函数的区别
- this指向
箭头函数没有自己的this
在箭头函数内调用this时,指向他的外层函数的this,如果他外层没有函数,那就指向全局对象,且this指向不可变,由定义时决定 - prototype
没有自己的prototype,所以不能用new来构建对象
会报错这个函数is not a constructor - arguments
不能使用arguments对象
arguments是一个类数组对象,不是数组,只能使用数组的length属性和索引,其他数组方法都不能用
在箭头函数内调用arguments时,指向他的外层函数的arguments,如果他外层没有函数,那就会报错arguments is not defined
替代:
用rest参数
let fn = (...args) => console.log(args);
fn('南木元元', 18);
- 不能用作generator
因为箭头函数内部不能使用yield
let fn = *() => {
yield '南木元元';
}
let p = fn();
console.log(p.next());