🔰arrow function
(ES6) a new form of function expression.
JS ⟩ value ⟩ function ⟩ arrow function
⭐️ ES6 (2015):a new form of function expression.
a => expression // one statement: {...} optional
(a, b) => expression // multiple params: (...) required
a => { statement1; statement2; } // multiple statements: {...} requiredwhen the { ... } are omitted, a return value is sent out without using return.
⭐️ arrow function as method❗️ (❗️use with caution❗️)
normally, you don't use arrow functions as methods, but if you have:
an object A that has a method returning a new object B,
object B has its own methods,
you don't want this in these methods to refer to B but refer to A instead,
then using arrow functions as methods is an option.
👉 see example: Sequence
don't have access to new.target keyword.
can’t be used as constructors. (can’t be called with new)
can’t use
yield, within its body.
deplicate parameters Early Error rule (🚧) is always applied to arrow function definition even if 'strict mode' (🚧) is not defined explicitly.
👉 see arrow function returning object literal needs (...)❗️
const user = () => { name: 'Gandalf', age: 2019 };
// ❌ wrong syntax ↳ ⛔️ SyntaxError: Unexpected token ':'
const user = () => ({ name: 'Gandalf', age: 2019 });
// ✅ wrap object literal in parenthesescan be used as a bound method in class field.
can be used in IIFE.
register handler with arrow functions will lose
thiscontext.
arr.containsSubarray() - used in Array.prototype.every().
isCurrentlyInGlobalScope() - check if current context is in global scope.
Last updated
Was this helpful?