箭頭函數至關於匿名函數,而且簡化了函數定義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 帶來的重大改進,在正確的場合使用箭頭函數能讓代碼變的簡潔、短小,但某些方面的優點在另一些方面可能就變成了劣勢,在須要動態上下文的場景中使用箭頭函數你要格外的當心,這些場景包括:定義對象方法、定義原型方法、定義構造函數、定義事件回調函數。函數