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/09-classes/01-class/article.md
+6-26Lines changed: 6 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -331,7 +331,7 @@ alert(user.name); // John
331
331
alert(User.prototype.name); // undefined
332
332
```
333
333
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:
335
335
336
336
```js run
337
337
class User {
@@ -344,6 +344,7 @@ let user = new User();
344
344
alert(user.name); // John
345
345
```
346
346
347
+
347
348
### Making bound methods with class fields
348
349
349
350
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`".
375
376
There are two approaches to fixing it, as discussed in the chapter <info:bind>:
376
377
377
378
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
-
classButton {
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 =newButton("hello");
395
-
396
-
*!*
397
-
setTimeout(button.click, 1000); // hello
398
-
*/!*
399
-
```
379
+
2. Bind the method to object, e.g. in the constructor.
400
380
401
-
Class fields provide a more elegant syntax for the latter solution:
381
+
Class fields provide another, quite elegant syntax:
402
382
403
383
```js run
404
384
classButton {
@@ -417,9 +397,9 @@ let button = new Button("hello");
417
397
setTimeout(button.click, 1000); // hello
418
398
```
419
399
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.
421
401
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.
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
+
classAnimal {
301
+
name ='animal'
302
+
303
+
constructor() {
304
+
alert(this.name); // (*)
305
+
}
306
+
}
307
+
308
+
classRabbitextendsAnimal {
309
+
name ='rabbit';
310
+
}
311
+
312
+
newAnimal(); // animal
313
+
*!*
314
+
newRabbit(); // 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
+
classAnimal {
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
+
classRabbitextendsAnimal {
344
+
showName() {
345
+
alert('rabbit');
346
+
}
347
+
}
348
+
349
+
newAnimal(); // animal
350
+
*!*
351
+
newRabbit(); // 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.
0 commit comments