箭头函数不仅在语法上比传统函数简洁的区别之外,还有很多不一样的地方。他比较适合那些使用匿名函数表达式的地方

  • 不能作为构造函数,用new去创建。不然会报错
let test = () => ({a: 1});

new test();
index.html:27 Uncaught TypeError: test is not a constructor
at index.html:27
  • 返回对象的时候要使用()括起来

比如这样写,其实是不能返回一个对象的

let test = () => {a: 1};

执行结果

undefined

正确的写法

let test = () => ({a: 1});
  • 没有prototype属性,也是undefined
  • 没有this
  • 没有arguments
  • 换行的时候在箭头和参数之前不能换行,不然会报错
Uncaught SyntaxError: Unexpected token '=>'
  • call()、apply()、bind()无法改变箭头函数中this的指向
var id = 'global';

var test = () => {
console.log(this.id);
}

test.call({id: 'aaa'});

执行结果还是global

  • 箭头函数不能使用yield和generator