add advantages for arrow examples (#18911)
explained benefits with code examples
This commit is contained in:
committed by
Quincy Larson
parent
32bd1a273d
commit
f53940c5c0
@ -45,7 +45,9 @@ let newOneWithOneParam = a => {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
An incredible advantage of the arrows function is that you can not rebind an arrow function. It will always be called with the context in which it was defined. Just use a normal function.
|
### advantages of arrow sytnax
|
||||||
|
1. It will always be called with the context in which it was defined. Therefore there is no existence of the this keyword.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
// Old Syntax
|
// Old Syntax
|
||||||
axios.get(url).then(function(response) {
|
axios.get(url).then(function(response) {
|
||||||
@ -56,9 +58,18 @@ axios.get(url).then(function(response) {
|
|||||||
axios.get(url).then(response => {
|
axios.get(url).then(response => {
|
||||||
this.data = response.data;
|
this.data = response.data;
|
||||||
});
|
});
|
||||||
|
|
||||||
```
|
```
|
||||||
|
* before arrow functions each function had it's own this contect
|
||||||
|
2. shorter more readable functions
|
||||||
|
```javascript
|
||||||
|
// Old Syntax
|
||||||
|
const sumValues = function (a, b) {
|
||||||
|
return a+b;
|
||||||
|
}
|
||||||
|
|
||||||
|
// New Syntax
|
||||||
|
const sumValues = (a,b) => a+b;
|
||||||
|
```
|
||||||
|
* since it's a one line return we can ommit the brackets
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
I don’t think I need to give an explanation for this. It's straightforward.
|
|
||||||
|
Reference in New Issue
Block a user