An online markdown blog and knowledge repository.
ECMAScript 2015 aka ES6 Arrow Functions notes taken while reading MDN and caniuse.com
MDN Docs: Arrow Function Expressions
Recall that the function keyword defines a Function Expression.
The syntax is: function [name]([param1[,paramN]]) { statements; };
[Mozilla Developer Network Documentation]
Name is the plain-english (or complicated gibberish) word by which the function can be referenced.
Parameters represent arguments to be passed in to the function.
There can be zero or one or many parameters.
A Code Block of statement is executed when the function is 'called'.
Functions always return something:
A compact alternative to function expressions.
Not-so-good Aspects:
About using 'this' keyword with functions and arrow functions:
param => expression
Note the following:
let myWord = 'bar';
function foo(word){
return `FOO ${word}.`;
}
let bar = word => `FOO-FOO ${word}.`;
bar (myWord);
'FOO-FOO bar.'
ES 6.0 defines a function as a variable that uses the keyword 'function', followed by parentheses for a parameters list, followed by a code block.
Arrow Functions:
Example:
function randomNumber() {
return Math.random;
}
...can be rewritten as an arrow function:
randomNum = () => Math.random;
Parenthesis are required to contain the input parameters, or no parameters at all.
(parm1, parm2) => expression
() => expression
Braces are not required for single statement expressions that do not overflow to the next line.
Multi-line statements require:
() => {
let expression1 = expression;
return expression1;
}
param => {
let result = expression + param;
return result;
}
Straight from [MDN, 'Arrow_functions']:
params => ({foo: "a"}) // returns object {foo: 'a'}
Object Literals have many uses, including:
Arrow Functions are useful for many purposes including:
An example Event Handler registration using an Arrow Function:
// JS Function method:
document.addEventListener('click', function() {
console.log('Click');
})
// Arrow Function method:
document.addEventListener('click', () => console.log('Click'));
Neat!
Arrow Functions can be written in several different ways, with options on when to use parenthesis and braces.
Parentheses:
/* zero parameters requires parens */
const alpha = () => `Hello World!`;
/* multiple params requires parens */
const bravo = (name, age) => `${name} is ${age} years old.`;
/* single param does not require parens */
const charlie = name => `Her name is ${name}.`;
/* surround object literal with parens */
const delta = (name, age) => ({
name: name,
age: age,
});
Braces:
/* single code statement does not require braces */
let echo = () => `Hello Hello World World!`;
/* multi-line code block requires braces */
let foxtrot = (length, width) => {
let ttlLen = length * 2;
let ttlWid = width * 2;
let perimeter = ttlLen + ttlWid;
return `Perimeter: ${perimeter}`;
}
Support for ES6 Arrow Functions is pretty vast, even on mobile and specialty platforms.
Note: IE (of course) and Opera Mini are two outliers as they do not support arrow functions.
WebDevSimplified ES6 Arrow Functions Tutorial on Youtube
Back to readme.md