More syntax examples (#18647)

Breaks down the different ways you can write arrow functions for those new to it
This commit is contained in:
Paul
2018-10-13 03:18:53 -04:00
committed by Aditya
parent 303ff4eea0
commit 721f799f28

View File

@ -27,6 +27,23 @@ var multiply = (x, y) => x * y;
You no longer need the `function` and `return` keywords, or even the curly brackets.
```javascript
// everything included
const multiply = function(x, y) => { return x * y };
// remove "function"
const multiply = (x, y) => { return x * y };
// remove curly brackets and "return" ==> this way it returns implicitly
const multiply = (x, y) => x * y;
// if you only have one argument/parameter
const multiplyBy2 = x => x * 2;
// combined with the ternary operator, but note it's not a looker!
const addOrMultiply = (x, y, mathOperator) => mathOperator.toLowerCase() === 'add' ? x + y : x * y;
```
### A simplified `this`
Before arrow functions, new functions defined their own `this` value. To use `this` inside a traditional function expression, we have to write a workaround like so:
@ -63,4 +80,4 @@ var p = new Person();
#### Further Reading
<a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions' target='_blank' rel='nofollow'>MDN link</a>
<a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions' target='_blank' rel='nofollow'>MDN link</a>