Skip to content

Commit 1ce1c59

Browse files
authored
cleanup on the ..arguments
Some lint changes but also adding a bit of discussion about ..args
1 parent 07d633d commit 1ce1c59

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

ebook/02_arrow_functions.md

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Chapter 2: Arrow functions
22

3-
## What is an arrow function
3+
## What is an arrow function?
44

55
ES6 introduced fat arrows (`=>`) as a way to declare functions.
66
This is how we would normally declare a function in ES5:
@@ -43,7 +43,7 @@ const greeting = () => {
4343
With arrow functions we can skip the explicit return and return like this:
4444

4545
``` javascript
46-
const greeting = (name) => `hello ${name}` ;
46+
const greeting = name => `hello ${name}`;
4747
```
4848

4949
Let's say we want to implicitly return an **object literal**, we would do like this:
@@ -52,7 +52,7 @@ Let's say we want to implicitly return an **object literal**, we would do like t
5252
const race = "100m dash";
5353
const runners = [ "Usain Bolt", "Justin Gatlin", "Asafa Powell" ];
5454

55-
const winner = runners.map(( runner, i) => ({ name: runner, race, place: i + 1}));
55+
const winner = runners.map((runner, i) => ({ name: runner, race, place: i + 1}));
5656

5757
console.log(winner);
5858
// 0: {name: "Usain Bolt", race: "100m dash", place: 1}
@@ -63,7 +63,7 @@ console.log(winner);
6363

6464
To tell JavaScript that what's inside the curly braces is an **object literal** that we want to implicitly return, we need to wrap everything inside parenthesis.
6565

66-
Writing `race` or `race:race` is the same.
66+
Writing `race` or `race: race` is the same.
6767

6868
 
6969

@@ -74,7 +74,7 @@ As you can see from the previous examples, arrow functions are **anonymous**.
7474
If we want to have a name to reference them we can bind them to a variable:
7575

7676
``` javascript
77-
const greeting = (name) => `hello ${name}`;
77+
const greeting = name => `hello ${name}`;
7878

7979
greeting("Tom");
8080
```
@@ -94,14 +94,14 @@ This can be useful in cases like this one:
9494
// grab our div with class box
9595
const box = document.querySelector(".box");
9696
// listen for a click event
97-
box.addEventListener("click",function() {
97+
box.addEventListener("click", function() {
9898
// toggle the class opening on the div
9999
this.classList.toggle("opening");
100100
setTimeout(function(){
101101
// try to toggle again the class
102102
this.classList.toggle("open");
103-
})
104-
})
103+
});
104+
});
105105
```
106106

107107

@@ -117,14 +117,14 @@ Since we know that **arrow functions** inherit the value of `this` from the pare
117117
// grab our div with class box
118118
const box = document.querySelector(".box");
119119
// listen for a click event
120-
box.addEventListener("click",function() {
120+
box.addEventListener("click", function () {
121121
// toggle the class opening on the div
122122
this.classList.toggle("opening");
123123
setTimeout(() => {
124-
// try to toggle again the class
124+
// try to toggle again the class
125125
this.classList.toggle("open");
126-
})
127-
})
126+
});
127+
});
128128
```
129129

130130
Here, the second `this` will inherit from its parent, and will be therefore set to the `const` box.
@@ -151,20 +151,19 @@ const person = {
151151
age: 10,
152152
grow: () => {
153153
// error: *this* refers to the window
154-
this.age ++,
155-
},
154+
this.age++,
155+
},
156156
}
157157
```
158158

159-
Here's another example of when you should use a normal function instead of an arrow.
159+
One other difference between Arrow functions and normal functions is access to the `arguments`.
160160

161-
``` javascript
161+
```javascript
162162
const orderRunners = () => {
163163
const runners = Array.from(arguments);
164164
return runners.map((runner, i) => {
165-
return ` #{runner} was number #{i +1}`;
166-
})
167-
console.log(arguments);
165+
return `#{runner} was number #{i +1}`;
166+
})
168167
}
169168
```
170169

@@ -174,4 +173,16 @@ This code will return:
174173
ReferenceError: arguments is not defined
175174
```
176175

177-
We don't have access to the `arguments` object in arrow functions, we need to use a normal function.
176+
To access all the arguments of an array, use old function notation, or the splat syntax. The name of the parameter does not matter (here it is called args to reduce confusion with `arguments`).
177+
178+
```javascript
179+
const orderRunners = (...args) => {
180+
const runners = Array.from(args);
181+
console.log(runners);
182+
return runners.map((runner, i) => {
183+
return `#{runner} was number #{i +1}`;
184+
})
185+
}
186+
187+
```
188+

0 commit comments

Comments
 (0)