This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We've got a `Clock` class. As of now, it prints the time every second.
Nous avons une classe `Clock`. À partir de maintenant, il affiche l'heure à chaque seconde.
[js src="source.view/clock.js"]
Create a new class `ExtendedClock` that inherits from `Clock` and adds the parameter `precision` -- the number of `ms` between "ticks". Should be `1000` (1 second) by default.
Créez une nouvelle classe `ExtendedClock` qui hérite de `Clock` et ajoute le paramètre `precision` - le nombre de `ms` entre "ticks". Devrait être `1000` (1 seconde) par défaut.
- Your code should be in the file `extended-clock.js`
- Don't modify the original `clock.js`. Extend it.
- Votre code devrait être dans le fichier `extended-clock.js`
- Ne modifiez pas le fichier `clock.js` d'origine. Étendez-le.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First, let's see why the latter code doesn't work.
Voyons d'abord pourquoi ce dernier code ne fonctionne pas.
The reason becomes obvious if we try to run it. An inheriting class constructor must call `super()`. Otherwise `"this"` won't be "defined".
La raison devient évidente si nous essayons de l'exécuter. Un constructeur de classe héritant doit appeler `super()`. Sinon `"this"` ne sera pas "défini".
So here's the fix:
Alors, voici la solution:
```js run
class Rabbit extends Object {
constructor(name) {
*!*
super(); // need to call the parent constructor when inheriting
super(); // besoin d'appeler le constructeur parent lors de l'héritage
*/!*
this.name = name;
}
Expand All
@@ -19,16 +19,16 @@ let rabbit = new Rabbit("Rab");
alert( rabbit.hasOwnProperty('name') ); // true
```
But that's not all yet.
Mais ce n'est pas tout.
Even after the fix, there's still important difference in `"class Rabbit extends Object"` versus `class Rabbit`.
Même après le correctif, il existe toujours une différence importante entre `"class Rabbit extends Object"` et `class Rabbit`.
As we know, the "extends" syntax sets up two prototypes:
Comme on le sait, la syntaxe "extend" configure deux prototypes:
1. Between `"prototype"` of the constructor functions (for methods).
2. Between the constructor functions itself (for static methods).
1. Entre le `"prototype"` des fonctions du constructeur (pour les méthodes).
2. Entre les fonctions du constructeur lui-même (pour les méthodes statiques).
In our case, for `class Rabbit extends Object` it means:
Dans notre cas, pour `class Rabbit extends Object`, cela signifie:
So `Rabbit` doesn't provide access to static methods of `Object` in that case.
Donc, `Rabbit` ne donne pas accès aux méthodes statiques de `Object` dans ce cas.
By the way, `Function.prototype` has "generic" function methods, like `call`, `bind` etc. They are ultimately available in both cases, because for the built-in `Object` constructor, `Object.__proto__ === Function.prototype`.
En passant, `Function.prototype` a des méthodes de fonction "génériques", comme `call`, `bind`, etc. Elles sont finalement disponibles dans les deux cas, car pour le constructeur `Object` intégré, `Object.__proto__ === Function.prototype`.
Here's the picture:
Voici l'image:

So, to put it short, there are two differences:
Donc, pour faire court, il y a deux différences:
| class Rabbit | class Rabbit extends Object |
|--------------|------------------------------|
| -- | needs to call `super()` in constructor |
| -- | doit appeler `super()` dans le constructeur |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
As we know, all objects normally inherit from `Object.prototype` and get access to "generic" object methods like `hasOwnProperty` etc.
Comme nous le savons, tous les objets héritent normalement de `Object.prototype` et ont accès à des méthodes d'objet "génériques" comme `hasOwnProperty` etc.
For instance:
Par exemple:
```js run
class Rabbit {
Expand All
@@ -18,17 +18,17 @@ class Rabbit {
let rabbit = new Rabbit("Rab");
*!*
// hasOwnProperty method is from Object.prototype
// la méthode hasOwnProperty est de Object.prototype
// rabbit.__proto__ === Object.prototype
alert( rabbit.hasOwnProperty('name') ); // true
*/!*
```
But if we spell it out explicitly like `"class Rabbit extends Object"`, then the result would be different from a simple `"class Rabbit"`?
Mais si nous l’épelons explicitement comme suit: `"class Rabbit extends Object"`, le résultat serait alors différent d´un simple `"class Rabbit"`?
What's the difference?
Quelle est la différence?
Here's an example of such code (it doesn't work -- why? fix it?):
Voici un exemple d'un tel code (cela ne fonctionne pas - pourquoi? Réparez le?):
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Class inheritance #74
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Class inheritance #74
Filter by extension
Viewed files
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
Uh oh!
There was an error while loading. Please reload this page.