Skip to content

Commit cb57bf0

Browse files
authored
Merge pull request #69 from lenchen1112/master
Sync with upstream @601d958
2 parents 0ca0cb8 + 3cd5b06 commit cb57bf0

File tree

49 files changed

+179
-163
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+179
-163
lines changed

1-js/03-code-quality/01-debugging-chrome/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ There are buttons for it at the top of the right panel. Let's engage them.
168168
<span class="devtools" style="background-position:-62px -192px"></span> -- "Step over": run the next command, but *don't go into a function*, hotkey `key:F10`.
169169
: Similar to the previous the "Step" command, but behaves differently if the next statement is a function call. That is: not a built-in, like `alert`, but a function of our own.
170170

171-
The "Step" command goes into it and and pauses the execution at its first line, while "Step over" executes the nested function call invisibly, skipping the function internals.
171+
The "Step" command goes into it and pauses the execution at its first line, while "Step over" executes the nested function call invisibly, skipping the function internals.
172172

173173
The execution is then paused immediately after that function.
174174

1-js/05-data-types/03-string/article.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
394394

395395
```js run
396396
let str = "st*!*ringify*/!*";
397-
alert( str.slice(2) ); // ringify, from the 2nd position till the end
397+
alert( str.slice(2) ); // 'ringify', from the 2nd position till the end
398398
```
399399

400400
Negative values for `start/end` are also possible. They mean the position is counted from the string end:
@@ -403,7 +403,7 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
403403
let str = "strin*!*gif*/!*y";
404404

405405
// start at the 4th position from the right, end at the 1st from the right
406-
alert( str.slice(-4, -1) ); // gif
406+
alert( str.slice(-4, -1) ); // 'gif'
407407
```
408408

409409
`str.substring(start [, end])`
@@ -435,14 +435,14 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
435435

436436
```js run
437437
let str = "st*!*ring*/!*ify";
438-
alert( str.substr(2, 4) ); // ring, from the 2nd position get 4 characters
438+
alert( str.substr(2, 4) ); // 'ring', from the 2nd position get 4 characters
439439
```
440440

441441
The first argument may be negative, to count from the end:
442442

443443
```js run
444444
let str = "strin*!*gi*/!*fy";
445-
alert( str.substr(-4, 2) ); // gi, from the 4th position get 2 characters
445+
alert( str.substr(-4, 2) ); // 'gi', from the 4th position get 2 characters
446446
```
447447

448448
Let's recap these methods to avoid any confusion:

1-js/05-data-types/05-array-methods/4-sort-back/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ importance: 4
22

33
---
44

5-
# Sort in the reverse order
5+
# Sort in decreasing order
66

77
```js
88
let arr = [5, 2, 1, -10, 8];
99

10-
// ... your code to sort it in the reverse order
10+
// ... your code to sort it in decreasing order
1111

1212
alert( arr ); // 8, 5, 2, 1, -10
1313
```

1-js/05-data-types/05-array-methods/article.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ That's natural, because `delete obj.key` removes a value by the `key`. It's all
3636

3737
So, special methods should be used.
3838

39-
The [arr.splice(str)](mdn:js/Array/splice) method is a swiss army knife for arrays. It can do everything: insert, remove and replace elements.
39+
The [arr.splice(start)](mdn:js/Array/splice) method is a swiss army knife for arrays. It can do everything: insert, remove and replace elements.
4040

4141
The syntax is:
4242

@@ -268,7 +268,7 @@ alert( arr.includes(NaN) );// true (correct)
268268

269269
Imagine we have an array of objects. How do we find an object with the specific condition?
270270

271-
Here the [arr.find](mdn:js/Array/find) method comes in handy.
271+
Here the [arr.find(fn)](mdn:js/Array/find) method comes in handy.
272272

273273
The syntax is:
274274
```js
@@ -574,7 +574,7 @@ The calculation flow:
574574

575575
Or in the form of a table, where each row represents a function call on the next array element:
576576

577-
| |`sum`|`current`|`result`|
577+
| |`sum`|`current`|result|
578578
|---|-----|---------|---------|
579579
|the first call|`0`|`1`|`1`|
580580
|the second call|`1`|`2`|`3`|

1-js/05-data-types/09-keys-values-entries/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ They are supported for:
1111

1212
- `Map`
1313
- `Set`
14-
- `Array` (except `arr.values()`)
14+
- `Array`
1515

1616
Plain objects also support similar methods, but the syntax is a bit different.
1717

1-js/05-data-types/10-destructuring-assignment/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ The destructuring assignment also works with objects.
187187
The basic syntax is:
188188

189189
```js
190-
let {var1, var2} = {var1:…, var2…}
190+
let {var1, var2} = {var1:…, var2:…}
191191
```
192192

193193
We have an existing object at the right side, that we want to split into variables. The left side contains a "pattern" for corresponding properties. In the simple case, that's a list of variable names in `{...}`.

1-js/05-data-types/12-json/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,9 @@ alert( numbers[1] ); // 1
425425
Or for nested objects:
426426

427427
```js run
428-
let user = '{ "name": "John", "age": 35, "isAdmin": false, "friends": [0,1,2,3] }';
428+
let userData = '{ "name": "John", "age": 35, "isAdmin": false, "friends": [0,1,2,3] }';
429429

430-
user = JSON.parse(user);
430+
let user = JSON.parse(userData);
431431

432432
alert( user.friends[1] ); // 1
433433
```

1-js/06-advanced-functions/01-recursion/article.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ let list = { value: 1 };
459459
list.next = { value: 2 };
460460
list.next.next = { value: 3 };
461461
list.next.next.next = { value: 4 };
462+
list.next.next.next.next = null;
462463
```
463464
464465
Here we can even more clearer see that there are multiple objects, each one has the `value` and `next` pointing to the neighbour. The `list` variable is the first object in the chain, so following `next` pointers from it we can reach any element.

1-js/06-advanced-functions/03-closure/article.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ Please note the additional `[[Environment]]` property is covered here. We didn't
351351

352352
![](lexenv-nested-makecounter-3.svg)
353353

354-
Please note that on this step the inner function was created, but not yet called. The code inside `function() { return count++; }` is not running.
354+
Please note that on this step the inner function was created, but not yet called. The code inside `return count++;` is not running.
355355

356356
4. As the execution goes on, the call to `makeCounter()` finishes, and the result (the tiny nested function) is assigned to the global variable `counter`:
357357

@@ -565,7 +565,8 @@ function f() {
565565
*/!*
566566
}
567567

568-
let g = f(); // g is reachable, and keeps the outer lexical environment in memory
568+
let func = f(); // func gets a reference to g
569+
// so it stays and memory and its outer lexical environment stays as well
569570
```
570571

571572
Please note that if `f()` is called many times, and resulting functions are saved, then all corresponding Lexical Environment objects will also be retained in memory. All 3 of them in the code below:
@@ -595,10 +596,9 @@ function f() {
595596
return g;
596597
}
597598

598-
let g = f(); // while g is alive
599-
// their corresponding Lexical Environment lives
599+
let func = f(); // while func has a reference to g, it stays in memory
600600

601-
g = null; // ...and now the memory is cleaned up
601+
func = null; // ...and now the memory is cleaned up
602602
```
603603

604604
### Real-life optimizations

1-js/06-advanced-functions/08-settimeout-setinterval/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ For server-side JavaScript, that limitation does not exist, and there exist othe
288288

289289
- Methods `setTimeout(func, delay, ...args)` and `setInterval(func, delay, ...args)` allow us to run the `func` once/regularly after `delay` milliseconds.
290290
- To cancel the execution, we should call `clearTimeout/clearInterval` with the value returned by `setTimeout/setInterval`.
291-
- Nested `setTimeout` calls is a more flexible alternative to `setInterval`, allowing us to set the time *between* executions more precisely.
291+
- Nested `setTimeout` calls are a more flexible alternative to `setInterval`, allowing us to set the time *between* executions more precisely.
292292
- Zero delay scheduling with `setTimeout(func, 0)` (the same as `setTimeout(func)`) is used to schedule the call "as soon as possible, but after the current script is complete".
293293
- The browser limits the minimal delay for five or more nested call of `setTimeout` or for `setInterval` (after 5th call) to 4ms. That's for historical reasons.
294294

1-js/06-advanced-functions/09-call-apply-decorators/04-throttle/_js.view/test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,20 @@ describe("throttle(f, 1000)", function() {
4444
this.clock.restore();
4545
});
4646

47-
});
47+
});
48+
49+
describe('throttle', () => {
50+
51+
it('runs a forwarded call once', done => {
52+
let log = '';
53+
const f = str => log += str;
54+
const f10 = throttle(f, 10);
55+
f10('once');
56+
57+
setTimeout(() => {
58+
assert.equal(log, 'once');
59+
done();
60+
}, 20);
61+
});
62+
63+
});

1-js/06-advanced-functions/10-bind/6-ask-partial/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The task is a little more complex variant of <info:task/question-use-bind>.
88

99
The `user` object was modified. Now instead of two functions `loginOk/loginFail`, it has a single function `user.login(true/false)`.
1010

11-
What to pass `askPassword` in the code below, so that it calls `user.login(true)` as `ok` and `user.login(false)` as `fail`?
11+
What should we pass `askPassword` in the code below, so that it calls `user.login(true)` as `ok` and `user.login(false)` as `fail`?
1212

1313
```js
1414
function askPassword(ok, fail) {

1-js/06-advanced-functions/10-bind/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Functions provide a built-in method [bind](mdn:js/Function/bind) that allows to
9898
The basic syntax is:
9999

100100
```js
101-
// more complex syntax will be little later
101+
// more complex syntax will come a little later
102102
let boundFunc = func.bind(context);
103103
```
104104

1-js/06-advanced-functions/12-arrow-functions/article.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ Here we had to create additional variables `args` and `ctx` so that the function
118118

119119
Arrow functions:
120120

121-
- Do not have `this`.
122-
- Do not have `arguments`.
123-
- Can't be called with `new`.
124-
- (They also don't have `super`, but we didn't study it. Will be in the chapter <info:class-inheritance>).
121+
- Do not have `this`
122+
- Do not have `arguments`
123+
- Can't be called with `new`
124+
- They also don't have `super`, but we didn't study it yet. We will on the chapter <info:class-inheritance>
125125

126-
That's because they are meant for short pieces of code that do not have their own "context", but rather works in the current one. And they really shine in that use case.
126+
That's because they are meant for short pieces of code that do not have their own "context", but rather work in the current one. And they really shine in that use case.

1-js/07-object-properties/01-property-descriptors/article.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
As we know, objects can store properties.
55

6-
Till now, a property was a simple "key-value" pair to us. But an object property is actually a more flexible and powerful thing.
6+
Until now, a property was a simple "key-value" pair to us. But an object property is actually a more flexible and powerful thing.
77

88
In this chapter we'll study additional configuration options, and in the next we'll see how to invisibly turn them into getter/setter functions.
99

@@ -134,7 +134,7 @@ let user = { };
134134
Object.defineProperty(user, "name", {
135135
*!*
136136
value: "John",
137-
// for new properties need to explicitly list what's true
137+
// for new properties we need to explicitly list what's true
138138
enumerable: true,
139139
configurable: true
140140
*/!*
@@ -148,7 +148,7 @@ user.name = "Pete"; // Error
148148

149149
Now let's add a custom `toString` to `user`.
150150

151-
Normally, a built-in `toString` for objects is non-enumerable, it does not show up in `for..in`. But if we add `toString` of our own, then by default it shows up in `for..in`, like this:
151+
Normally, a built-in `toString` for objects is non-enumerable, it does not show up in `for..in`. But if we add a `toString` of our own, then by default it shows up in `for..in`, like this:
152152

153153
```js run
154154
let user = {
@@ -162,7 +162,7 @@ let user = {
162162
for (let key in user) alert(key); // name, toString
163163
```
164164

165-
If we don't like it, then we can set `enumerable:false`. Then it won't appear in `for..in` loop, just like the built-in one:
165+
If we don't like it, then we can set `enumerable:false`. Then it won't appear in a `for..in` loop, just like the built-in one:
166166

167167
```js run
168168
let user = {

1-js/07-object-properties/02-property-accessors/article.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
There are two kinds of properties.
55

6-
The first kind is *data properties*. We already know how to work with them. All properties that we've been using till now were data properties.
6+
The first kind is *data properties*. We already know how to work with them. All properties that we've been using until now were data properties.
77

88
The second type of properties is something new. It's *accessor properties*. They are essentially functions that work on getting and setting a value, but look like regular properties to an external code.
99

@@ -189,9 +189,9 @@ Technically, external code is able to access the name directly by using `user._n
189189

190190
## Using for compatibility
191191

192-
One of the great uses of accessors -- they allow to take control over a "regular" data property at any moment by replacing it with getter and setter and tweak its behavior.
192+
One of the great uses of accessors is that they allow to take control over a "regular" data property at any moment by replacing it with a getter and a setter and tweak its behavior.
193193

194-
Imagine, we started implementing user objects using data properties `name` and `age`:
194+
Imagine we started implementing user objects using data properties `name` and `age`:
195195

196196
```js
197197
function User(name, age) {

1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ importance: 5
66

77
The task has two parts.
88

9-
We have objects:
9+
Given the following objects:
1010

1111
```js
1212
let head = {

1-js/08-prototypes/01-prototype-inheritance/3-proto-and-this/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ importance: 5
22

33
---
44

5-
# Where it writes?
5+
# Where does it write?
66

77
We have `rabbit` inheriting from `animal`.
88

1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Why two hamsters are full?
5+
# Why are both hamsters full?
66

77
We have two hamsters: `speedy` and `lazy` inheriting from the general `hamster` object.
88

9-
When we feed one of them, the other one is also full. Why? How to fix it?
9+
When we feed one of them, the other one is also full. Why? How can we fix it?
1010

1111
```js run
1212
let hamster = {

1-js/08-prototypes/01-prototype-inheritance/article.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ rabbit.__proto__ = animal;
3434
```smart header="`__proto__` is a historical getter/setter for `[[Prototype]]`"
3535
Please note that `__proto__` is *not the same* as `[[Prototype]]`. That's a getter/setter for it.
3636

37-
It exists for historical reasons, in modern language it is replaced with functions `Object.getPrototypeOf/Object.setPrototypeOf` that also get/set the prototype. We'll study the reasons for that and these functions later.
37+
It exists for historical reasons. In modern language it is replaced with functions `Object.getPrototypeOf/Object.setPrototypeOf` that also get/set the prototype. We'll study the reasons for that and these functions later.
3838

3939
By the specification, `__proto__` must only be supported by browsers, but in fact all environments including server-side support it. For now, as `__proto__` notation is a little bit more intuitively obvious, we'll use it in the examples.
4040
```
@@ -203,7 +203,7 @@ Here in the line `(*)` the property `admin.fullName` has a getter in the prototy
203203

204204
## The value of "this"
205205

206-
An interesting question may arise in the example above: what's the value of `this` inside `set fullName(value)`? Where the properties `this.name` and `this.surname` are written: into `user` or `admin`?
206+
An interesting question may arise in the example above: what's the value of `this` inside `set fullName(value)`? Where are the properties `this.name` and `this.surname` written: into `user` or `admin`?
207207

208208
The answer is simple: `this` is not affected by prototypes at all.
209209

@@ -246,13 +246,13 @@ The resulting picture:
246246

247247
![](proto-animal-rabbit-walk-3.svg)
248248

249-
If we had other objects like `bird`, `snake` etc inheriting from `animal`, they would also gain access to methods of `animal`. But `this` in each method call would be the corresponding object, evaluated at the call-time (before dot), not `animal`. So when we write data into `this`, it is stored into these objects.
249+
If we had other objects, like `bird`, `snake`, etc., inheriting from `animal`, they would also gain access to methods of `animal`. But `this` in each method call would be the corresponding object, evaluated at the call-time (before dot), not `animal`. So when we write data into `this`, it is stored into these objects.
250250

251251
As a result, methods are shared, but the object state is not.
252252

253253
## for..in loop
254254

255-
The `for..in` loops over inherited properties too.
255+
The `for..in` loop iterates over inherited properties too.
256256

257257
For instance:
258258

@@ -267,7 +267,7 @@ let rabbit = {
267267
};
268268

269269
*!*
270-
// Object.keys only return own keys
270+
// Object.keys only returns own keys
271271
alert(Object.keys(rabbit)); // jumps
272272
*/!*
273273

1-js/08-prototypes/02-function-prototype/1-changing-prototype/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ alert( rabbit.eats ); // true
2020
```
2121

2222

23-
1. We added one more string (emphasized), what `alert` shows now?
23+
1. We added one more string (emphasized). What will `alert` show now?
2424

2525
```js
2626
function Rabbit() {}
@@ -54,7 +54,7 @@ alert( rabbit.eats ); // true
5454
alert( rabbit.eats ); // ?
5555
```
5656

57-
3. Like this (replaced one line)?
57+
3. And like this (replaced one line)?
5858

5959
```js
6060
function Rabbit() {}

1-js/08-prototypes/02-function-prototype/article.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Remember, new objects can be created with a constructor function, like `new F()`.
44

5-
If `F.prototype` is an object, then `new` operator uses it to set `[[Prototype]]` for the new object.
5+
If `F.prototype` is an object, then the `new` operator uses it to set `[[Prototype]]` for the new object.
66

77
```smart
88
JavaScript had prototypal inheritance from the beginning. It was one of the core features of the language.
@@ -158,9 +158,9 @@ Rabbit.prototype = {
158158

159159
In this chapter we briefly described the way of setting a `[[Prototype]]` for objects created via a constructor function. Later we'll see more advanced programming patterns that rely on it.
160160

161-
Everything is quite simple, just few notes to make things clear:
161+
Everything is quite simple, just a few notes to make things clear:
162162

163-
- The `F.prototype` property (don't mess with `[[Prototype]]`) sets `[[Prototype]]` of new objects when `new F()` is called.
163+
- The `F.prototype` property (don't mistake it for `[[Prototype]]`) sets `[[Prototype]]` of new objects when `new F()` is called.
164164
- The value of `F.prototype` should be either an object or `null`: other values won't work.
165165
- The `"prototype"` property only has such a special effect when set on a constructor function, and invoked with `new`.
166166

0 commit comments

Comments
 (0)