箭头函数和普通函数的区别
原创
©著作权归作者所有:来自51CTO博客作者wx62ca408a2c56b的原创作品,请联系作者获取转载授权,否则将追究法律责任
箭头函数不仅在语法上比传统函数简洁的区别之外,还有很多不一样的地方。他比较适合那些使用匿名函数表达式的地方
let test = () => ({a: 1});
new test();
index.html:27 Uncaught TypeError: test is not a constructor
at index.html:27
比如这样写,其实是不能返回一个对象的
执行结果
正确的写法
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