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: 1-js/02-first-steps/13-while-for/article.md
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -318,7 +318,7 @@ alert('Done!');
318
318
319
319
We need a way to stop the process if the user cancels the input.
320
320
321
-
The ordinary `break` after `input` would only break the inner loop. That's not sufficient--labels, come to the rescue!
321
+
The ordinary `break` after `input` would only break the inner loop. That's not sufficient -- labels, come to the rescue!
322
322
323
323
A *label* is an identifier with a colon before a loop:
324
324
```js
@@ -363,12 +363,14 @@ Labels do not allow us to jump into an arbitrary place in the code.
363
363
364
364
For example, it is impossible to do this:
365
365
```js
366
-
break label; // doesn't jumps to the label below
366
+
break label; // jump to the label below (doesn't work)
367
367
368
368
label: for (...)
369
369
```
370
370
371
-
A call to `break/continue` is only possible from inside a loop and the label must be somewhere above the directive.
371
+
A call to `continue` is only possible from inside the loop.
372
+
373
+
The `break` directive may be placed before code blocks too, as `label: { ... }`, but it's almost never used like that. And it also works only inside-out.
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/02-object-copy/article.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -100,7 +100,7 @@ alert( a == b ); // false
100
100
101
101
For comparisons like `obj1 > obj2` or for a comparison against a primitive `obj ==5`, objects are converted to primitives. We'll study how object conversions work very soon, but to tell the truth, such comparisons are needed very rarely -- usually they appear as a result of a programming mistake.
102
102
103
-
## Cloning and merging, Object.assign
103
+
## Cloning and merging, Object.assign [#cloning-and-merging-object-assign]
104
104
105
105
So, copying an object variable creates one more reference to the same object.
106
106
@@ -186,7 +186,7 @@ let clone = Object.assign({}, user);
186
186
187
187
It copies all properties of `user` into the empty object and returns it.
188
188
189
-
There are also other methods of cloning an object, e.g. using the [spread operator](info:rest-parameters-spread) `clone = {...user}`, covered later in the tutorial.
189
+
There are also other methods of cloning an object, e.g. using the [spread syntax](info:rest-parameters-spread) `clone = {...user}`, covered later in the tutorial.
2. The method [toFixed(n)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) rounds the number to `n` digits after the point and returns a string representation of the result.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/06-function-object/article.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
2
2
# Function object, NFE
3
3
4
-
As we already know, functions in JavaScript are values.
4
+
As we already know, a function in JavaScript is a value.
5
5
6
6
Every value in JavaScript has a type. What type is a function?
7
7
@@ -12,7 +12,7 @@ A good way to imagine functions is as callable "action objects". We can not only
12
12
13
13
## The "name" property
14
14
15
-
Function objects contain a few useable properties.
15
+
Function objects contain some useable properties.
16
16
17
17
For instance, a function's name is accessible as the "name" property:
18
18
@@ -24,14 +24,14 @@ function sayHi() {
24
24
alert(sayHi.name); // sayHi
25
25
```
26
26
27
-
What's more funny, the name-assigning logic is smart. It also assigns the correct name to functions that are used in assignments:
27
+
What's kind of funny, the name-assigning logic is smart. It also assigns the correct name to a function even if it's created without one, and then immediately assigned:
28
28
29
29
```js run
30
30
letsayHi=function() {
31
31
alert("Hi");
32
-
}
32
+
};
33
33
34
-
alert(sayHi.name); // sayHi (works!)
34
+
alert(sayHi.name); // sayHi (there's a name!)
35
35
```
36
36
37
37
It also works if the assignment is done via a default value:
@@ -93,7 +93,7 @@ alert(many.length); // 2
93
93
94
94
Here we can see that rest parameters are not counted.
95
95
96
-
The `length` property is sometimes used for introspection in functions that operate on other functions.
96
+
The `length` property is sometimes used for [introspection](https://en.wikipedia.org/wiki/Type_introspection) in functions that operate on other functions.
97
97
98
98
For instance, in the code below the `ask` function accepts a `question` to ask and an arbitrary number of `handler` functions to call.
99
99
@@ -102,9 +102,9 @@ Once a user provides their answer, the function calls the handlers. We can pass
102
102
- A zero-argument function, which is only called when the user gives a positive answer.
103
103
- A function with arguments, which is called in either case and returns an answer.
104
104
105
-
The idea is that we have a simple, no-arguments handler syntax for positive cases (most frequent variant), but are able to provide universal handlers as well.
105
+
To call `handler` the right way, we examine the `handler.length` property.
106
106
107
-
To call `handlers` the right way, we examine the `length` property:
107
+
The idea is that we have a simple, no-arguments handler syntax for positive cases (most frequent variant), but are able to support universal handlers as well:
108
108
109
109
```js run
110
110
functionask(question, ...handlers) {
@@ -153,7 +153,7 @@ alert( `Called ${sayHi.counter} times` ); // Called 2 times
153
153
```warn header="A property is not a variable"
154
154
A property assigned to a function like `sayHi.counter = 0` does *not* define a local variable `counter` inside it. In other words, a property `counter` and a variable `let counter` are two unrelated things.
155
155
156
-
We can treat a function as an object, store properties in it, but that has no effect on its execution. Variables never use function properties and vice versa. These are just parallel worlds.
156
+
We can treat a function as an object, store properties in it, but that has no effect on its execution. Variables are not function properties and vice versa. These are just parallel worlds.
157
157
```
158
158
159
159
Function properties can replace closures sometimes. For instance, we can rewrite the counter function example from the chapter <info:closure> to use a function property:
@@ -241,7 +241,7 @@ let sayHi = function *!*func*/!*(who) {
241
241
sayHi("John"); // Hello, John
242
242
```
243
243
244
-
There are two special things about the name `func`:
244
+
There are two special things about the name `func`, that are the reasons for it:
245
245
246
246
1. It allows the function to reference itself internally.
247
247
2. It is not visible outside of the function.
@@ -282,7 +282,7 @@ let sayHi = function(who) {
282
282
};
283
283
```
284
284
285
-
The problem with that code is that the value of `sayHi` may change. The function may go to another variable, and the code will start to give errors:
285
+
The problem with that code is that `sayHi` may change in the outer code. If the function gets assigned to another variable instead, the code will start to give errors:
Now it works, because the name `"func"` is function-local. It is not taken from outside (and not visible there). The specification guarantees that it will always reference the current function.
328
328
329
-
The outer code still has it's variable `sayHi` or `welcome`. And `func` is an "internal function name", how the function can call itself internally.
329
+
The outer code still has its variable `sayHi` or `welcome`. And `func` is an "internal function name", how the function can call itself internally.
330
330
331
331
```smart header="There's no such thing for Function Declaration"
332
332
The "internal name" feature described here is only available for Function Expressions, not for Function Declarations. For Function Declarations, there is no syntax for adding an "internal" name.
@@ -340,7 +340,7 @@ Functions are objects.
340
340
341
341
Here we covered their properties:
342
342
343
-
-`name` -- the function name. Exists not only when given in the function definition, but also for assignments and object properties.
343
+
-`name` -- the function name. Usually taken from the function definition, but if there's none, JavaScript tries to guess it from the context (e.g. an assignment).
344
344
-`length` -- the number of arguments in the function definition. Rest parameters are not counted.
345
345
346
346
If the function is declared as a Function Expression (not in the main code flow), and it carries the name, then it is called a Named Function Expression. The name can be used inside to reference itself, for recursive calls or such.
Static methods are used for the functionality that belongs to the class "as a whole". It doesn't relate to a concrete class instance.
202
206
203
-
## Summary
207
+
For example, a method for comparison `Article.compare(article1, article2)` or a factory method `Article.createTodays()`.
204
208
205
-
Static methods are used for the functionality that doesn't relate to a concrete class instance, doesn't require an instance to exist, but rather belongs to the class as a whole, like `Article.compare` -- a generic method to compare two articles.
209
+
They are labeled by the word `static` in class declaration.
206
210
207
211
Static properties are used when we'd like to store class-level data, also not bound to an instance.
208
212
@@ -218,13 +222,13 @@ class MyClass {
218
222
}
219
223
```
220
224
221
-
That's technically the same as assigning to the class itself:
225
+
Technically, static declaration is the same as assigning to the class itself:
222
226
223
227
```js
224
228
MyClass.property=...
225
229
MyClass.method=...
226
230
```
227
231
228
-
Static properties are inherited.
232
+
Static properties and methods are inherited.
229
233
230
-
Technically, for`class B extends A` the prototype of the class `B` itself points to `A`: `B.[[Prototype]] = A`. So if a field is not found in `B`, the search continues in `A`.
234
+
For`class B extends A` the prototype of the class `B` itself points to `A`: `B.[[Prototype]] = A`. So if a field is not found in `B`, the search continues in `A`.
Copy file name to clipboardExpand all lines: 1-js/10-error-handling/1-try-catch/1-finally-or-code-after/solution.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
The difference becomes obvious when we look at the code inside a function.
2
2
3
-
The behavior is different if there's a "jump out" of `try..catch`.
3
+
The behavior is different if there's a "jump out" of `try...catch`.
4
4
5
-
For instance, when there's a `return` inside `try..catch`. The `finally` clause works in case of *any* exit from `try..catch`, even via the `return` statement: right after `try..catch` is done, but before the calling code gets the control.
5
+
For instance, when there's a `return` inside `try...catch`. The `finally` clause works in case of *any* exit from `try...catch`, even via the `return` statement: right after `try...catch` is done, but before the calling code gets the control.
6
6
7
7
```js run
8
8
functionf() {
@@ -11,7 +11,7 @@ function f() {
11
11
*!*
12
12
return"result";
13
13
*/!*
14
-
} catch (e) {
14
+
} catch (err) {
15
15
/// ...
16
16
} finally {
17
17
alert('cleanup!');
@@ -28,11 +28,11 @@ function f() {
28
28
try {
29
29
alert('start');
30
30
thrownewError("an error");
31
-
} catch (e) {
31
+
} catch (err) {
32
32
// ...
33
33
if("can't handle the error") {
34
34
*!*
35
-
throwe;
35
+
throwerr;
36
36
*/!*
37
37
}
38
38
@@ -44,4 +44,4 @@ function f() {
44
44
f(); // cleanup!
45
45
```
46
46
47
-
It's `finally` that guarantees the cleanup here. If we just put the code at the end of `f`, it wouldn't run.
47
+
It's `finally` that guarantees the cleanup here. If we just put the code at the end of `f`, it wouldn't run in these situations.
0 commit comments