You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
65
65
66
-
Writing `race` or `race:race` is the same.
66
+
Writing `race` or `race:race` is the same.
67
67
68
68
69
69
@@ -74,7 +74,7 @@ As you can see from the previous examples, arrow functions are **anonymous**.
74
74
If we want to have a name to reference them we can bind them to a variable:
75
75
76
76
```javascript
77
-
constgreeting=(name)=>`hello ${name}`;
77
+
constgreeting=name=>`hello ${name}`;
78
78
79
79
greeting("Tom");
80
80
```
@@ -94,14 +94,14 @@ This can be useful in cases like this one:
94
94
// grab our div with class box
95
95
constbox=document.querySelector(".box");
96
96
// listen for a click event
97
-
box.addEventListener("click",function() {
97
+
box.addEventListener("click",function() {
98
98
// toggle the class opening on the div
99
99
this.classList.toggle("opening");
100
100
setTimeout(function(){
101
101
// try to toggle again the class
102
102
this.classList.toggle("open");
103
-
})
104
-
})
103
+
});
104
+
});
105
105
```
106
106
107
107
@@ -117,14 +117,14 @@ Since we know that **arrow functions** inherit the value of `this` from the pare
117
117
// grab our div with class box
118
118
constbox=document.querySelector(".box");
119
119
// listen for a click event
120
-
box.addEventListener("click",function() {
120
+
box.addEventListener("click",function() {
121
121
// toggle the class opening on the div
122
122
this.classList.toggle("opening");
123
123
setTimeout(() => {
124
-
// try to toggle again the class
124
+
// try to toggle again the class
125
125
this.classList.toggle("open");
126
-
})
127
-
})
126
+
});
127
+
});
128
128
```
129
129
130
130
Here, the second `this` will inherit from its parent, and will be therefore set to the `const` box.
@@ -151,20 +151,19 @@ const person = {
151
151
age:10,
152
152
grow: () => {
153
153
// error: *this* refers to the window
154
-
this.age++,
155
-
},
154
+
this.age++,
155
+
},
156
156
}
157
157
```
158
158
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`.
160
160
161
-
```javascript
161
+
```javascript
162
162
constorderRunners= () => {
163
163
construnners=Array.from(arguments);
164
164
returnrunners.map((runner, i) => {
165
-
return` #{runner} was number #{i +1}`;
166
-
})
167
-
console.log(arguments);
165
+
return`#{runner} was number #{i +1}`;
166
+
})
168
167
}
169
168
```
170
169
@@ -174,4 +173,16 @@ This code will return:
174
173
ReferenceError:arguments is not defined
175
174
```
176
175
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`).
0 commit comments