Skip to content

Commit 9bba300

Browse files
committed
minor
1 parent 1bb1293 commit 9bba300

File tree

12 files changed

+11
-9
lines changed

12 files changed

+11
-9
lines changed
56.4 KB
Loading
89.9 KB
Loading

1-js/07-object-oriented-programming/10-class-inheritance/article.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ At the line `(*)` we take `eat` from the prototype (`animal`) and call it in the
301301

302302
And in the code above it actually works as intended: we have the correct `alert`.
303303

304-
Now let's add one more object to the chain. And we'll see how things break:
304+
Now let's add one more object to the chain. We'll see how things break:
305305

306306
```js run
307307
let animal = {
@@ -336,20 +336,22 @@ The code doesn't work any more! We can see the error trying to call `longEar.eat
336336

337337
It may be not that obvious, but if we trace `longEar.eat()` call, then we can see why. In both lines `(*)` and `(**)` the value of `this` is the current object (`longEar`). That's essential: all object methods get the current object as `this`, not a prototype or something.
338338

339-
So, in both lines `(*)` and `(**)` the value of `this.__proto__` is exactly the same: `rabbit`. They both call `rabbit.eat` without going up the chain.
339+
So, in both lines `(*)` and `(**)` the value of `this.__proto__` is exactly the same: `rabbit`. They both call `rabbit.eat` without going up the chain in the endless loop.
340340

341-
In other words:
341+
Here's the picture of what happens:
342342

343-
1. Inside `longEar.eat()`, we pass the call up to `rabbit.eat` giving it the same `this=longEar`.
343+
![](this-super-loop.png)
344+
345+
1. Inside `longEar.eat()`, the line `(**)` calls `rabbit.eat` providing it with `this=longEar`.
344346
```js
345347
// inside longEar.eat() we have this = longEar
346348
this.__proto__.eat.call(this) // (**)
347349
// becomes
348350
longEar.__proto__.eat.call(this)
349-
// or
351+
// that is
350352
rabbit.eat.call(this);
351353
```
352-
2. Inside `rabbit.eat`, we want to pass the call even higher in the chain, but `this=longEar`, so `this.__proto__.eat` is `rabbit.eat`!
354+
2. Then in the line `(*)` of `rabbit.eat`, we'd like to pass the call even higher in the chain, but `this=longEar`, so `this.__proto__.eat` is again `rabbit.eat`!
353355
354356
```js
355357
// inside rabbit.eat() we also have this = longEar
@@ -362,9 +364,7 @@ In other words:
362364
363365
3. ...So `rabbit.eat` calls itself in the endless loop, because it can't ascend any further.
364366

365-
![](this-super-loop.png)
366-
367-
There problem is unsolvable, because `this` must always be the calling object itself, no matter which parent method is called. So its prototype will always be the immediate parent of the object. We can't go up the chain.
367+
The problem can't be solved by using `this` alone.
368368
369369
### `[[HomeObject]]`
370370
@@ -501,6 +501,8 @@ alert(Rabbit.prototype.__proto__ === Animal.prototype);
501501

502502
This way `Rabbit` has access to all static methods of `Animal`.
503503

504+
### No static inheritance in built-ins
505+
504506
Please note that built-in classes don't have such static `[[Prototype]]` reference. For instance, `Object` has `Object.defineProperty`, `Object.keys` and so on, but `Array`, `Date` etc do not inherit them.
505507

506508
Here's the picture structure for `Date` and `Object`:
1.51 KB
Loading
4.17 KB
Loading
-20.5 KB
Loading
-46.2 KB
Loading
-87 Bytes
Loading
-159 Bytes
Loading
-35 Bytes
Loading

0 commit comments

Comments
 (0)