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/02-class-inheritance/article.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -209,7 +209,7 @@ With constructors it gets a little bit tricky.
209
209
210
210
Till now, `Rabbit` did not have its own `constructor`.
211
211
212
-
According to the [specification](https://tc39.github.io/ecma262/#sec-runtime-semantics-classdefinitionevaluation), if a class extends another class and has no `constructor`, then the following `constructor` is generated:
212
+
According to the [specification](https://tc39.github.io/ecma262/#sec-runtime-semantics-classdefinitionevaluation), if a class extends another class and has no `constructor`, then the following "empty" `constructor` is generated:
Let's get a little deeper under the hood of `super`. We'll see some interesting things by the way.
311
311
312
-
First to say, from all that we've learned till now, it's impossible for `super` to work.
312
+
First to say, from all that we've learned till now, it's impossible for `super` to work at all!
313
313
314
-
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?
314
+
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, it needs to retrieve the `method`from the prototype of the current object. How JavaScript engine should get the prototype of `this`?
315
315
316
-
Maybe we can get the method from `[[Prototype]]` of `this`, as `this.__proto__.method`? Unfortunately, that doesn't work.
316
+
The task may seem simple, but it isn't. The engine could try to get the method from `[[Prototype]]` of `this`, as `this.__proto__.method`. Unfortunately, that doesn't work.
317
317
318
-
Let's try to do it. Without classes, using plain objects for the sake of simplicity.
318
+
Let's demonstrate the problem. Without classes, using plain objects for the sake of simplicity.
319
319
320
-
Here, `rabbit.eat()`should call `animal.eat()` method of the parent object:
320
+
In the example below, `rabbit.__proto__ = animal`. Now let's try: in `rabbit.eat()`we'll call `animal.eat()`, using `this.__proto__`:
321
321
322
322
```js run
323
323
let animal = {
@@ -418,7 +418,7 @@ To provide the solution, JavaScript adds one more special internal property for
418
418
419
419
This actually violates the idea of "unbound" functions, because methods remember their objects. And `[[HomeObject]]` can't be changed, so this bound is forever. So that's a very important change in the language.
420
420
421
-
But this change is safe. `[[HomeObject]]` is used only for calling parent methods in `super`, to resolve the prototype. So it doesn't break compatibility.
421
+
But this change is safe. `[[HomeObject]]` is used only for calling parent methods in `super`, to resolve the prototype. So it doesn't breakcompatibility.Regular method calls know nothing about `[[HomeObject]]`, it only matters for`super`.
422
422
423
423
Let's see how it works for `super` -- again, using plain objects:
0 commit comments