箭头函数至关于匿名函数,而且简化了函数定义app
let FunConstructor = () => { console.log('lll'); } let fc = new FunConstructor(); //TypeError
let B = (b)=>{ console.log(arguments); } B(2,92,32,32); // Uncaught ReferenceError: arguments is not defined let C = (...c) => { console.log(c); } C(3,82,32,11323); // [3, 82, 32, 11323]
var obj = { a: 10, b: () => { console.log(this.a); // undefined console.log(this); // Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …} }, c: function() { console.log(this.a); // 10 console.log(this); // {a: 10, b: ƒ, c: ƒ} } } obj.b(); obj.c();
var a = ()=>{ return 1; } function b(){ return 2; } console.log(a.prototype); // undefined console.log(b.prototype); // {constructor: ƒ}
箭头函数无疑是 ES6 带来的重大改进,在正确的场合使用箭头函数能让代码变的简洁、短小,但某些方面的优点在另一些方面可能就变成了劣势,在须要动态上下文的场景中使用箭头函数你要格外的当心,这些场景包括:定义对象方法、定义原型方法、定义构造函数、定义事件回调函数。函数