Skip to content

Commit ab40409

Browse files
committed
fix
1 parent ccbc49e commit ab40409

File tree

1 file changed

+4
-3
lines changed
  • 1-js/07-object-oriented-programming/10-class-inheritance

1 file changed

+4
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ First to say, from all that we've learned till now, it's impossible for `super`
269269

270270
Yeah, indeed, let's ask ourselves, how it could technically work? When an object method runs, it gets the current object as `this`. If we call `super.method()` then, how to retrieve the `method`? Naturally, we need to take the `method` from the prototype of the current object. How, technically, we (or a JavaScript engine) can do it?
271271

272-
Maybe we can get it `[[Prototype]]` of `this`, as `this.__proto__.method`? Unfortunately, that won't work.
272+
Maybe we can get the method from `[[Prototype]]` of `this`, as `this.__proto__.method`? Unfortunately, that doesn't work.
273273

274274
Let's try to do it. Without classes, using plain objects for the sake of simplicity.
275275

@@ -288,6 +288,7 @@ let rabbit = {
288288
name: "Rabbit",
289289
eat() {
290290
*!*
291+
// that's how super.eat() could presumably work
291292
this.__proto__.eat.call(this); // (*)
292293
*/!*
293294
}
@@ -298,9 +299,9 @@ rabbit.eat(); // Rabbit eats.
298299

299300
At the line `(*)` we take `eat` from the prototype (`animal`) and call it in the context of the current object. Please note that `.call(this)` is important here, because a simple `this.__proto__.eat()` would execute parent `eat` in the context of the prototype, not the current object.
300301

301-
And here it works.
302+
And in the code above it actually works as intended: we have the correct `alert`.
302303

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

305306
```js run
306307
let animal = {

0 commit comments

Comments
 (0)