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
Copy file name to clipboardExpand all lines: ebook/03_default_function_arguments.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ It works, but it's not very nice, how to improve it?
30
30
With **destructuring** we can write this:
31
31
32
32
```javascript
33
-
constBill=calculatePrice({ tip:0.15, total:150});
33
+
constBill=calculatePrice({ tip:0.15, total:150});
34
34
```
35
35
36
36
We don't even have to pass the parameters in the same order as when we declared our function, since we are calling them the same way as the arguments JavaScript will know how to match them.
Copy file name to clipboardExpand all lines: ebook/13_promises.md
+4-5Lines changed: 4 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,6 @@ fs.readdir(source, function (err, files) {
48
48
})
49
49
}
50
50
})
51
-
52
51
```
53
52
54
53
We try to write our code in a way where executions happens visually from top to bottom, causing excessive nesting on functions and result in what you can see above.
@@ -227,12 +226,12 @@ promise1.then(data => {
227
226
console.log(data);
228
227
});
229
228
// after 500 ms
230
-
//my first value
229
+
// first value
231
230
promise2.then(data=> {
232
231
console.log(data);
233
232
});
234
233
// after 1000 ms
235
-
//my second value
234
+
// second value
236
235
```
237
236
238
237
They will resolve independently from one another but look at what happens when we use `Promise.all().`
@@ -245,10 +244,10 @@ Promise
245
244
console.log(promise1data, promise2data);
246
245
});
247
246
// after 1000 ms
248
-
//my first value my second value
247
+
// first value second value
249
248
```
250
249
251
-
Our values returned together, after 1000ms (the timeout of the *second* promise) meanin that the first one had to wait the completion of the second one.
250
+
Our values returned together, after 1000ms (the timeout of the *second* promise) meaning that the first one had to wait the completion of the second one.
252
251
253
252
If we were to pass an empty iterable then it will return an already resolved promise.
Copy file name to clipboardExpand all lines: ebook/14_generators.md
+8-6Lines changed: 8 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Chapter 14: Generators
2
2
3
-
## What is a Generator?
3
+
## What is a Generator?
4
4
5
5
A generator function is a function that we can start and stop, for an indefinite amount of time, and restart with the possibility of passing additional data at a later point in time.
6
6
@@ -66,7 +66,7 @@ fruitGenerator.next().value;
66
66
```
67
67
68
68
- Our new generator will loop over the array and print one value at a time every time we call `.next()`.
69
-
- if you are only concerned about getting the value then use `.next().value` and it will not print the status of the generator
69
+
- if you are only concerned about getting the value, then use `.next().value` and it will not print the status of the generator
70
70
71
71
72
72
@@ -120,12 +120,13 @@ As you can see when we called `.throw()` the `generator` returned us the error a
120
120
121
121
122
122
123
-
124
123
## Combining Generators with Promises
125
124
126
-
As we have previously seen, Promises are very useful for asynchronous programming and by combining them with generators we can have a very powerful tool at our disposal to avoid problems like the *callback hell*.
125
+
As we have previously seen, Promises are very useful for asynchronous programming, and by combining them with generators we can have a very powerful tool at our disposal to avoid problems like the *callback hell*.
126
+
127
+
As we are solely discussing ES6, I won't be talking about async functions as they were introduce in ES8 (ES2017) but know that the way they work is based on what you will see now.
127
128
128
-
As we are solely discussing ES6, I won't be talking about `async functions` as they were introduce in ES8 (ES2017) but know that the way they work is based on what you will see now.
129
+
You can read more about async functionsin Chapter 19.
129
130
130
131
Using a Generator in combination with a Promise will allow us to write asynchronous code that feels like synchronous.
The first time we call `.next()` it will call our promise and wait for it to resolve( in our simple example it resolves immediately) and when we call `.next()` again it will utilize the value returned by the promise to do something else(in this case just interpolate a string).
157
+
The first time we call `.next()` it will call our promise and wait for it to resolve (in our simple example it resolves immediately) and when we call `.next()` again it will utilize the value returned by the promise to do something else(in this case just interpolate a string).
Copy file name to clipboardExpand all lines: ebook/16_sets_weaksets_maps_weakmaps.md
+9-10Lines changed: 9 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,9 +26,9 @@ console.log(family);
26
26
// Set [ "Dad", "Mom", "Son" ]
27
27
```
28
28
29
-
As you can see, at the end we tried to add "Dad" againbut the `Set` still remained the same because a `Set` can only take **unique values**.
29
+
As you can see, at the end we tried to add "Dad" again,but the `Set` still remained the same because a `Set` can only take **unique values**.
30
30
31
-
Let's continue using the same `Set` and see what methd we can use on it.
31
+
Let's continue using the same `Set` and see what method we can use on it.
32
32
33
33
```js
34
34
family.size;
@@ -95,14 +95,14 @@ console.log(uniqueArray);
95
95
96
96
// write the same but in a single line
97
97
constuniqueArray=Array.from(newSet(myArray));
98
-
//// Array [ "dad", "mom", "son", "daughter" ]
98
+
// Array [ "dad", "mom", "son", "daughter" ]
99
99
```
100
100
101
101
As you can see the new array only contains the unique values from the original array.
102
102
103
103
104
104
105
-
## What is a `WeakSet`?
105
+
## What is a `WeakSet`?
106
106
107
107
A `WeakSet` is similar to a `Set` but it can **only** contain Objects.
108
108
@@ -138,9 +138,9 @@ As you can see after a few seconds **dad** was removed and *garbage collected*.
138
138
139
139
140
140
141
-
## What is a `Map`?
141
+
## What is a `Map`?
142
142
143
-
A `Map` is similar to a `Set` but they have key and value pairs.
143
+
A `Map` is similar to a `Set`, but they have key and value pairs.
144
144
145
145
```js
146
146
constfamily=newMap();
@@ -167,14 +167,13 @@ for(const [key,val] of family){
167
167
// Son 20
168
168
```
169
169
170
-
If you remember, we could iterate over a `Set` only with a `for of` loop while we can iterate over a `Map` with both a `for of` and a `forEach` loop.
171
-
170
+
If you remember, we could iterate over a `Set` only with a `for of` loop, while we can iterate over a `Map` with both a `for of` and a `forEach` loop.
172
171
173
172
174
173
175
-
## What is a `WeakMap`?
174
+
## What is a `WeakMap`?
176
175
177
-
A `WeakMap` is a collection of key/value pairs and similarly to a `WeakSet`, even in a `WeakMap` the keys are *weakly* referenced, which means that when the reference is lost the value will be removed from the `WeakMap` and *garbage collected*.
176
+
A `WeakMap` is a collection of key/value pairs and similarly to a `WeakSet`, even in a `WeakMap` the keys are *weakly* referenced, which means that when the reference is lost, the value will be removed from the `WeakMap` and *garbage collected*.
178
177
179
178
A `WeakMap` is **not** enumerable therefore we cannot loop over it.
Copy file name to clipboardExpand all lines: ebook/17_ES7_incudes-and-exponential-operator.md
+5-6Lines changed: 5 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,9 @@
1
1
# Chapter 17: Everything new in ES2016 (ES7)
2
2
3
-
4
-
5
3
ES2016 introduced only two new features :
6
4
7
5
-`Array.prototype.includes()`
8
-
-the exponential operator
6
+
-The exponential operator
9
7
10
8
11
9
@@ -21,13 +19,14 @@ array.includes(2);
21
19
array.includes(3);
22
20
// false
23
21
```
22
+
24
23
25
24
26
25
### Combine `includes()` with `fromIndex`
27
26
28
27
We can provide `.includes()` with an index where to begin searching for an element. Default is 0, but we can also pass a negative value.
29
28
30
-
The first value we pass is the element to search and the second one is the index:
29
+
The first value we pass in is the element to search and the second one is the index:
31
30
32
31
```js
33
32
let array = [1,3,5,7,9,11];
@@ -42,9 +41,9 @@ array.includes(11,-3);
42
41
// true
43
42
```
44
43
45
-
`array.includes(5,4);` returned `false` because, despite the array actually contains the number 5, it is at the index 2 but we started looking at position 4. That's why we couldn't find it and it returned `false`.
44
+
`array.includes(5,4);` returned `false` because, despite the array actually contains the number 5, it is found at the index 2 but we started looking at position 4. That's why we couldn't find it and it returned `false`.
46
45
47
-
`array.includes(1,-1);` returned `false` because we started looking at the index -1 (which is the last element of the array) and then continued from that point onwards.
46
+
`array.includes(1,-1);` returned `false` because we started looking at the index -1 (which is the last element of the array) and then continued from that point onward.
48
47
49
48
`array.includes(11,-3);` returned `true` because we went back to the index -3 and moved up, finding the value 11 on our path.
Copy file name to clipboardExpand all lines: ebook/18_ES8_string-padding-object-entries-object-values-and-more.md
+13-12Lines changed: 13 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ ES2017 introduced many new cool features, which we are going to see here. I will
4
4
5
5
## String padding(`padStart` and `padEnd`)
6
6
7
-
We can now add some padding to our strings, either at the end (`padEnd`) or at the beginning (`padStart`).
7
+
We can now add some padding to our strings, either at the end (`padEnd`) or at the beginning (`padStart`) of them.
8
8
9
9
```js
10
10
"hello".padStart(6);
@@ -13,10 +13,10 @@ We can now add some padding to our strings, either at the end (`padEnd`) or at t
13
13
// "hello "
14
14
```
15
15
16
-
We said we want 6 as our padding, but why in both cases we got only 1 space?
17
-
It happens because `padStart` and `padEnd` will go and fill the empty spaces. In our example "hello" is 5 letters, and our padding is 6, which leaves only 1 empty space.
16
+
We specified that we want 6 as our padding, but why in both cases we got only 1 space?
17
+
It happens because `padStart` and `padEnd` will go and fill the **empty spaces**. In our example "hello" is 5 letters, and our padding is 6, which leaves only 1 empty space.
18
18
19
-
Look at this example
19
+
Look at this example:
20
20
21
21
```js
22
22
"hi".padStart(10);
@@ -36,7 +36,8 @@ We can use `padStart` if we want to right align something.
36
36
```js
37
37
conststrings= ["short", "medium length", "very long string"];
`Object.values()` returns an array of all the values whilst `Object.entries()` returns an array of arrays containing both keys and values.
@@ -145,7 +146,7 @@ const object = {
145
146
```
146
147
147
148
Notice how I wrote a comma at the end of the second property.
148
-
It will not throw any error if you don't put it but it's a better practice to follow as it will make the life easier to your colleague or team members.
149
+
It will not throw any error if you don't put it, but it's a better practice to follow as it will make the life easier to your colleague or team members.
149
150
150
151
```js
151
152
// I write
@@ -160,7 +161,7 @@ const object = {
160
161
prop2:"propop"
161
162
prop3:"propopop"
162
163
}
163
-
// suddenly he gets an error because he did not notice that I forgot to leave a comma at the end of the last parameter.
164
+
// suddenly, he gets an error because he did not notice that I forgot to leave a comma at the end of the last parameter.
0 commit comments