Skip to content

Commit bad02b9

Browse files
committed
Updating README: Adding sections on Arrow Functions and Blocks
1 parent ddc0318 commit bad02b9

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,80 @@ This bug highlights that we need to be careful when refactoring legacy code whic
4343

4444
> **Best Practice**: Leave **var** declarations inside of legacy code to denote that it needs to be carefully refactored. When working on a new codebase, use **let** for variables that will change their value over time, and **const** for variables that will be immutable over time.
4545
46+
## Replacing IIFEs with Blocks
47+
48+
> A common use of **Immediately Invoked Function Expressions** is to enclose values within its scope. In ES6, we now have the ability to create block-based scopes and therefore are not limited purely to function-based scope.
49+
50+
```javascript
51+
(function () {
52+
var food = 'Meow Mix';
53+
}());
54+
console.log(food); // Reference Error
55+
```
56+
57+
Using ES6 Blocks:
58+
59+
```javascript
60+
{
61+
let food = 'Meow Mix';
62+
}
63+
console.log(food); // Reference Error
64+
```
65+
66+
## Arrow Functions
67+
68+
Often times we have nested functions in which we would like to preserve the context of **this** from it's lexical scope. An example is shown below:
69+
70+
```javascript
71+
function Person(name) {
72+
this.name = name;
73+
}
74+
75+
Person.prototype.prefixName = function (arr) {
76+
return arr.map(function (character) {
77+
return this.name + character; // Cannot read property 'name' of undefined
78+
});
79+
};
80+
```
81+
82+
One common solution to this problem is to store the context of **this** using a variable:
83+
84+
```javascript
85+
function Person(name) {
86+
this.name = name;
87+
}
88+
89+
Person.prototype.prefixName = function (arr) {
90+
var that = this; // Store the context of this
91+
return arr.map(function (character) {
92+
return that.name + character;
93+
});
94+
};
95+
```
96+
97+
Using **Arrow Functions**, the lexical value of **this** isn't shadowed and we can re-write the above as shown:
98+
99+
```javascript
100+
function Person(name) {
101+
this.name = name;
102+
}
103+
104+
Person.prototype.prefixName = function (arr) {
105+
return arr.map((character) => this.name + character);
106+
}
107+
```
108+
109+
> **Best Practice**: Use **Arrow Functions** whenever you need to preserve the lexical value of **this**.
110+
111+
Arrow Functions are also more concise when used in function expressions which simply return a value:
112+
113+
```javascript
114+
var squares = arr.map(function (x) { return x * x }); // Function Expression
115+
```
116+
117+
```javascript
118+
const arr = [1, 2, 3, 4, 5];
119+
const squares = arr.map(x => x * x); // Arrow Function for terser implementation
120+
```
121+
122+
> **Best Practice**: Use **Arrow Functions** in place of function expressions when possible.

0 commit comments

Comments
 (0)