Skip to content

Commit da9849d

Browse files
committedJul 1, 2020
fixes #1979
1 parent 340ce43 commit da9849d

File tree

2 files changed

+102
-26
lines changed

2 files changed

+102
-26
lines changed
 

‎1-js/09-classes/01-class/article.md

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ alert(user.name); // John
331331
alert(User.prototype.name); // undefined
332332
```
333333

334-
Technically, they are processed after the constructor has done it's job, and we can use for them complex expressions and function calls:
334+
We can also assign values using more complex expressions and function calls:
335335

336336
```js run
337337
class User {
@@ -344,6 +344,7 @@ let user = new User();
344344
alert(user.name); // John
345345
```
346346

347+
347348
### Making bound methods with class fields
348349

349350
As demonstrated in the chapter <info:bind> functions in JavaScript have a dynamic `this`. It depends on the context of the call.
@@ -375,30 +376,9 @@ The problem is called "losing `this`".
375376
There are two approaches to fixing it, as discussed in the chapter <info:bind>:
376377

377378
1. Pass a wrapper-function, such as `setTimeout(() => button.click(), 1000)`.
378-
2. Bind the method to object, e.g. in the constructor:
379-
380-
```js run
381-
class Button {
382-
constructor(value) {
383-
this.value = value;
384-
*!*
385-
this.click = this.click.bind(this);
386-
*/!*
387-
}
388-
389-
click() {
390-
alert(this.value);
391-
}
392-
}
393-
394-
let button = new Button("hello");
395-
396-
*!*
397-
setTimeout(button.click, 1000); // hello
398-
*/!*
399-
```
379+
2. Bind the method to object, e.g. in the constructor.
400380

401-
Class fields provide a more elegant syntax for the latter solution:
381+
Class fields provide another, quite elegant syntax:
402382

403383
```js run
404384
class Button {
@@ -417,9 +397,9 @@ let button = new Button("hello");
417397
setTimeout(button.click, 1000); // hello
418398
```
419399

420-
The class field `click = () => {...}` creates an independent function on each `Button` object, with `this` bound to the object. Then we can pass `button.click` around anywhere, and it will be called with the right `this`.
400+
The class field `click = () => {...}` is created on a per-object basis, there's a separate function for each `Button` object, with `this` inside it referencing that object. We can pass `button.click` around anywhere, and the value of `this` will always be correct.
421401

422-
That's especially useful in browser environment, when we need to setup a method as an event listener.
402+
That's especially useful in browser environment, for event listeners.
423403

424404
## Summary
425405

‎1-js/09-classes/02-class-inheritance/article.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,102 @@ alert(rabbit.earLength); // 10
279279
```
280280

281281

282+
283+
### Overriding class fields: a tricky note
284+
285+
```warn header="Advanced note"
286+
This note assumes you have a certain experience with classes, maybe in other programming languages.
287+
288+
It provides better insight into the language and also explains the behavior that might be a source of bugs (but not very often).
289+
290+
If you find it difficult to understand, just go on, continue reading, then return to it some time later.
291+
```
292+
293+
We can override not only methods, but also class fields.
294+
295+
Although, there's a tricky behavior when we access an overridden field in parent constructor, quite different from most other programming languages.
296+
297+
Consider this example:
298+
299+
```js run
300+
class Animal {
301+
name = 'animal'
302+
303+
constructor() {
304+
alert(this.name); // (*)
305+
}
306+
}
307+
308+
class Rabbit extends Animal {
309+
name = 'rabbit';
310+
}
311+
312+
new Animal(); // animal
313+
*!*
314+
new Rabbit(); // animal
315+
*/!*
316+
```
317+
318+
Here, class `Rabbit` extends `Animal` and overrides `name` field with its own value.
319+
320+
There's no own constructor in `Rabbit`, so `Animal` constructor is called.
321+
322+
What's interesting is that in both cases: `new Animal()` and `new Rabbit()`, the `alert` in the line `(*)` shows `animal`.
323+
324+
**In other words, parent constructor always uses its own field value, not the overridden one.**
325+
326+
What's odd about it?
327+
328+
If it's not clear yet, please compare with methods.
329+
330+
Here's the same code, but instead of `this.name` field we call `this.showName()` method:
331+
332+
```js run
333+
class Animal {
334+
showName() { // instead of this.name = 'animal'
335+
alert('animal');
336+
}
337+
338+
constructor() {
339+
this.showName(); // instead of alert(this.name);
340+
}
341+
}
342+
343+
class Rabbit extends Animal {
344+
showName() {
345+
alert('rabbit');
346+
}
347+
}
348+
349+
new Animal(); // animal
350+
*!*
351+
new Rabbit(); // rabbit
352+
*/!*
353+
```
354+
355+
Please note: now the output is different.
356+
357+
And that's what we naturally expect. When the parent constructor is called in the derived class, it uses the overridden method.
358+
359+
...But for class fields it's not so. As said, the parent constructor always uses the parent field.
360+
361+
Why is there the difference?
362+
363+
Well, the reason is in the field initialization order. The class field is initialized:
364+
- Before constructor for the base class (that doesn't extend anything),
365+
- Imediately after `super()` for the derived class.
366+
367+
In our case, `Rabbit` is the derived class. There's no `constructor()` in it. As said previously, that's the same as if there was an empty constructor with only `super(...args)`.
368+
369+
So, `new Rabbit()` calls `super()`, thus executing the parent constructor, and (per the rule for derived classes) only after that its class fields are initialized. At the time of the parent constructor execution, there are no `Rabbit` class fields yet, that's why `Animal` fields are used.
370+
371+
This subtle difference between fields and methods is specific to JavaScript
372+
373+
Luckily, this behavior only reveals itself if an overridden field is used in the parent constructor. Then it may be difficult to understand what's going on, so we're explaining it here.
374+
375+
If it becomes a problem, one can fix it by using methods or getters/setters instead of fields.
376+
377+
282378
## Super: internals, [[HomeObject]]
283379

284380
```warn header="Advanced information"

0 commit comments

Comments
 (0)
Please sign in to comment.