Skip to content

Commit fea2f34

Browse files
committed
Resolve conflicts
1 parent 0a8c9fc commit fea2f34

File tree

7 files changed

+59
-100
lines changed

7 files changed

+59
-100
lines changed

1-js/06-advanced-functions/06-function-object/article.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Function object, NFE
33

4-
As we already know, functions in JavaScript are values.
4+
As we already know, a function in JavaScript is a value.
55

66
Every value in JavaScript has a type. What type is a function?
77

@@ -12,7 +12,7 @@ A good way to imagine functions is as callable "action objects". We can not only
1212

1313
## The "name" property
1414

15-
Function objects contain a few useable properties.
15+
Function objects contain some useable properties.
1616

1717
For instance, a function's name is accessible as the "name" property:
1818

@@ -24,14 +24,14 @@ function sayHi() {
2424
alert(sayHi.name); // sayHi
2525
```
2626

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:
2828

2929
```js run
3030
let sayHi = function() {
3131
alert("Hi");
32-
}
32+
};
3333

34-
alert(sayHi.name); // sayHi (works!)
34+
alert(sayHi.name); // sayHi (there's a name!)
3535
```
3636

3737
It also works if the assignment is done via a default value:
@@ -93,7 +93,7 @@ alert(many.length); // 2
9393

9494
Here we can see that rest parameters are not counted.
9595

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.
9797

9898
For instance, in the code below the `ask` function accepts a `question` to ask and an arbitrary number of `handler` functions to call.
9999

@@ -102,9 +102,9 @@ Once a user provides their answer, the function calls the handlers. We can pass
102102
- A zero-argument function, which is only called when the user gives a positive answer.
103103
- A function with arguments, which is called in either case and returns an answer.
104104

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.
106106

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:
108108

109109
```js run
110110
function ask(question, ...handlers) {
@@ -153,7 +153,7 @@ alert( `Called ${sayHi.counter} times` ); // Called 2 times
153153
```warn header="A property is not a variable"
154154
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.
155155
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.
157157
```
158158

159159
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) {
241241
sayHi("John"); // Hello, John
242242
```
243243

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:
245245

246246
1. It allows the function to reference itself internally.
247247
2. It is not visible outside of the function.
@@ -282,7 +282,7 @@ let sayHi = function(who) {
282282
};
283283
```
284284

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:
286286

287287
```js run
288288
let sayHi = function(who) {
@@ -340,7 +340,7 @@ Functions are objects.
340340

341341
Here we covered their properties:
342342

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).
344344
- `length` -- the number of arguments in the function definition. Rest parameters are not counted.
345345

346346
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.

1-js/09-classes/03-static-properties-methods/article.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
# Static properties and methods
33

4-
We can also assign a method to the class function, not to its `"prototype"`. Such methods are called *static*.
4+
We can also assign a method to the class function itself, not to its `"prototype"`. Such methods are called *static*.
55

6-
An example:
6+
In a class, they are prepended by `static` keyword, like this:
77

88
```js run
99
class User {
@@ -17,7 +17,7 @@ class User {
1717
User.staticMethod(); // true
1818
```
1919

20-
That actually does the same as assigning it as a function property:
20+
That actually does the same as assigning it as a property directly:
2121

2222
```js run
2323
class User { }
@@ -29,11 +29,11 @@ User.staticMethod = function() {
2929
User.staticMethod(); // true
3030
```
3131

32-
The value of `this` inside `User.staticMethod()` is the class constructor `User` itself (the "object before dot" rule).
32+
The value of `this` in `User.staticMethod()` call is the class constructor `User` itself (the "object before dot" rule).
3333

3434
Usually, static methods are used to implement functions that belong to the class, but not to any particular object of it.
3535

36-
For instance, we have `Article` objects and need a function to compare them. The natural choice would be `Article.compare`, like this:
36+
For instance, we have `Article` objects and need a function to compare them. A natural solution would be to add `Article.compare` method, like this:
3737

3838
```js run
3939
class Article {
@@ -51,25 +51,25 @@ class Article {
5151

5252
// usage
5353
let articles = [
54-
new Article("Mind", new Date(2019, 1, 1)),
55-
new Article("Body", new Date(2019, 0, 1)),
54+
new Article("HTML", new Date(2019, 1, 1)),
55+
new Article("CSS", new Date(2019, 0, 1)),
5656
new Article("JavaScript", new Date(2019, 11, 1))
5757
];
5858

5959
*!*
6060
articles.sort(Article.compare);
6161
*/!*
6262

63-
alert( articles[0].title ); // Body
63+
alert( articles[0].title ); // CSS
6464
```
6565

66-
Here `Article.compare` stands "over" the articles, as a means to compare them. It's not a method of an article, but rather of the whole class.
66+
Here `Article.compare` stands "above" articles, as a means to compare them. It's not a method of an article, but rather of the whole class.
6767

6868
Another example would be a so-called "factory" method. Imagine, we need few ways to create an article:
6969

7070
1. Create by given parameters (`title`, `date` etc).
7171
2. Create an empty article with today's date.
72-
3. ...
72+
3. ...or else somehow.
7373

7474
The first way can be implemented by the constructor. And for the second one we can make a static method of the class.
7575

@@ -109,7 +109,7 @@ Article.remove({id: 12345});
109109

110110
[recent browser=Chrome]
111111

112-
Static properties are also possible, just like regular class properties:
112+
Static properties are also possible, they look like regular class properties, but prepended by `static`:
113113

114114
```js run
115115
class Article {
@@ -176,20 +176,24 @@ alert(Rabbit.planet); // Earth
176176

177177
Now when we call `Rabbit.compare`, the inherited `Animal.compare` will be called.
178178

179-
How does it work? Again, using prototypes. As you might have already guessed, extends also gives `Rabbit` the `[[Prototype]]` reference to `Animal`.
180-
179+
How does it work? Again, using prototypes. As you might have already guessed, `extends` gives `Rabbit` the `[[Prototype]]` reference to `Animal`.
181180

182181
![](animal-rabbit-static.svg)
183182

183+
So, `Rabbit extends Animal` creates two `[[Prototype]]` references:
184+
185+
1. `Rabbit` function prototypally inherits from `Animal` function.
186+
2. `Rabbit.prototype` prototypally inherits from `Animal.prototype`.
187+
184188
As a result, inheritance works both for regular and static methods.
185189

186-
Here, let's check that:
190+
Here, let's check that by code:
187191

188192
```js run
189193
class Animal {}
190194
class Rabbit extends Animal {}
191195

192-
// for static properties and methods
196+
// for statics
193197
alert(Rabbit.__proto__ === Animal); // true
194198

195199
// for regular methods
@@ -200,9 +204,9 @@ alert(Rabbit.prototype.__proto__ === Animal.prototype); // true
200204

201205
Static methods are used for the functionality that belongs to the class "as a whole". It doesn't relate to a concrete class instance.
202206

203-
## Summary
207+
For example, a method for comparison `Article.compare(article1, article2)` or a factory method `Article.createTodays()`.
204208

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.
206210

207211
Static properties are used when we'd like to store class-level data, also not bound to an instance.
208212

@@ -218,13 +222,13 @@ class MyClass {
218222
}
219223
```
220224

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:
222226

223227
```js
224228
MyClass.property = ...
225229
MyClass.method = ...
226230
```
227231

228-
Static properties are inherited.
232+
Static properties and methods are inherited.
229233

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`.

1-js/10-error-handling/1-try-catch/1-finally-or-code-after/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ function f() {
4444
f(); // cleanup!
4545
```
4646

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.

1-js/10-error-handling/1-try-catch/1-finally-or-code-after/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ Compare the two code fragments.
3333
*/!*
3434
```
3535

36-
We definitely need the cleanup after the work has started, doesn't matter if there was an error or not.
36+
We definitely need the cleanup after the work, doesn't matter if there was an error or not.
3737
3838
Is there an advantage here in using `finally` or both code fragments are equal? If there is such an advantage, then give an example when it matters.

0 commit comments

Comments
 (0)