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

1. 箭头函数没有 prototype 属性

let fn = () => {};
fn.prototype === undefined; //true

2. 箭头函数的 this 指向定义时外层第一个普通函数的 this

let fn,
  barObj = { message: 111 },
  fooObj = { message: 222 };
function bar() {
  // 运行时实例
  fn();
}

function foo() {
  // 定义时实例
  fn = () => {
    console.log("输出:", this); // 输出: {message: 222}
  };
}

foo.call(fooObj);
bar.call(barObj);

3. 不能直接修改箭头函数的 this

4. 当箭头函数外层没有普通函数时,它的 this 在严格和非严格模式都是指向 window

5. 箭头函数没有外层函数直接使用 arguments 会报错

6. 箭头函数有外层函数时,arguments 继承自外层函数的 arguments

7. 使用 new 调用箭头函数会报错

贡献者: huxiguo