Skip to content

Commit e8d2c88

Browse files
committed
Translates Class checking: instanceof into French
1 parent 0f21937 commit e8d2c88

File tree

3 files changed

+94
-94
lines changed

3 files changed

+94
-94
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Yeah, looks strange indeed.
1+
Ouais, ça a l'air étrange.
22

3-
But `instanceof` does not care about the function, but rather about its `prototype`, that it matches against the prototype chain.
3+
Mais `instanceof` ne se soucie pas de la fonction, mais plutôt de son `prototype`, qui correspond à la chaîne de prototypes.
44

5-
And here `a.__proto__ == B.prototype`, so `instanceof` returns `true`.
5+
Et ici `a.__ proto__ == B.prototype`, ainsi `instanceof` renvoie `true`.
66

7-
So, by the logic of `instanceof`, the `prototype` actually defines the type, not the constructor function.
7+
Ainsi, par la logique de `instanceof`, le `prototype` définit en fait le type, pas la fonction constructeur.

1-js/09-classes/06-instanceof/1-strange-instanceof/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 5
22

33
---
44

5-
# Strange instanceof
5+
# instanceof étrange
66

7-
Why `instanceof` below returns `true`? We can easily see that `a` is not created by `B()`.
7+
Pourquoi `instanceof` ci-dessous renvoie `true`? Nous pouvons facilement voir que `a` n'est pas créé par `B()`.
88

99
```js run
1010
function A() {}
Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,168 @@
1-
# Class checking: "instanceof"
1+
# Vérification de classe: "instanceof"
22

3-
The `instanceof` operator allows to check whether an object belongs to a certain class. It also takes inheritance into account.
3+
L'opérateur `instanceof` permet de vérifier si un objet appartient à une certaine classe. Il prend également en compte l'héritage.
44

5-
Such a check may be necessary in many cases, here we'll use it for building a *polymorphic* function, the one that treats arguments differently depending on their type.
5+
Une telle vérification peut être nécessaire dans de nombreux cas. Nous l'utilisons ici pour construire une fonction *polymorphique*, celle qui traite les arguments différemment en fonction de leur type.
66

7-
## The instanceof operator [#ref-instanceof]
7+
## L'opérateur instanceof [#ref-instanceof]
88

9-
The syntax is:
9+
La syntaxe est la suivante:
1010
```js
1111
obj instanceof Class
1212
```
1313

14-
It returns `true` if `obj` belongs to the `Class` or a class inheriting from it.
14+
Cela renvoie `true` si `obj` appartient à la `Class` ou à une classe qui en hérite.
1515

16-
For instance:
16+
Par exemple:
1717

1818
```js run
1919
class Rabbit {}
2020
let rabbit = new Rabbit();
2121

22-
// is it an object of Rabbit class?
22+
// est-ce un objet de la classe Rabbit?
2323
*!*
2424
alert( rabbit instanceof Rabbit ); // true
2525
*/!*
2626
```
2727

28-
It also works with constructor functions:
28+
Cela fonctionne aussi avec les fonctions constructeur:
2929

3030
```js run
3131
*!*
32-
// instead of class
32+
// au lieu de classe
3333
function Rabbit() {}
3434
*/!*
3535

3636
alert( new Rabbit() instanceof Rabbit ); // true
3737
```
3838

39-
...And with built-in classes like `Array`:
39+
...Et avec des classes intégrées comme `Array`:
4040

4141
```js run
4242
let arr = [1, 2, 3];
4343
alert( arr instanceof Array ); // true
4444
alert( arr instanceof Object ); // true
4545
```
4646

47-
Please note that `arr` also belongs to the `Object` class. That's because `Array` prototypally inherits from `Object`.
47+
Veuillez noter que `arr` appartient également à la classe `Object`. C'est parce que `Array` hérite prototypiquement de `Object`.
4848

49-
Normally, `instanceof` operator examines the prototype chain for the check. We can also set a custom logic in the static method `Symbol.hasInstance`.
49+
Normalement, l’opérateur `instanceof` examine la chaîne de prototypes pour le contrôle. Nous pouvons également définir une logique personnalisée dans la méthode statique `Symbol.hasInstance`.
5050

51-
The algorithm of `obj instanceof Class` works roughly as follows:
51+
L'algorithme de `obj instanceof Class` fonctionne à peu près comme suit:
5252

53-
1. If there's a static method `Symbol.hasInstance`, then just call it: `Class[Symbol.hasInstance](obj)`. It should return either `true` or `false`, and we're done. That's how we can customize the behavior of `instanceof`.
53+
1. S'il existe une méthode statique `Symbol.hasInstance`, appelez-la simplement: `Class[Symbol.hasInstance](obj)`. Cela devrait renvoyer `true` ou `false`, et nous avons terminé. C'est ainsi que nous pouvons personnaliser le comportement de `instanceof`.
5454

55-
For example:
55+
     Par exemple:
5656

57-
```js run
58-
// setup instanceOf check that assumes that
59-
// anything with canEat property is an animal
60-
class Animal {
61-
static [Symbol.hasInstance](obj) {
62-
if (obj.canEat) return true;
63-
}
64-
}
57+
```js run
58+
// configuration du contrôle de instanceOf qui suppose que
59+
// tout ce qui a la propriété canEat est un animal
60+
class Animal {
61+
static [Symbol.hasInstance](obj) {
62+
if (obj.canEat) return true;
63+
}
64+
}
6565

66-
let obj = { canEat: true };
66+
let obj = { canEat: true };
6767

68-
alert(obj instanceof Animal); // true: Animal[Symbol.hasInstance](obj) is called
69-
```
68+
alert(obj instanceof Animal); // true: Animal[Symbol.hasInstance](obj) est appelée
69+
```
7070

71-
2. Most classes do not have `Symbol.hasInstance`. In that case, the standard logic is used: `obj instanceOf Class` checks whether `Class.prototype` equals to one of prototypes in the `obj` prototype chain.
71+
2. La plupart des classes n'ont pas `Symbol.hasInstance`. Dans ce cas, la logique standard est utilisée: `obj instanceOf Class` vérifie si `Class.prototype` est égal à l'un des prototypes de la chaîne de prototypes `obj`.
7272

73-
In other words, compare one after another:
74-
```js
75-
obj.__proto__ === Class.prototype?
76-
obj.__proto__.__proto__ === Class.prototype?
77-
obj.__proto__.__proto__.__proto__ === Class.prototype?
78-
...
79-
// if any answer is true, return true
80-
// otherwise, if we reached the end of the chain, return false
81-
```
73+
En d'autres termes, comparez les uns après les autres:
74+
```js
75+
obj.__proto__ === Class.prototype?
76+
obj.__proto__.__proto__ === Class.prototype?
77+
obj.__proto__.__proto__.__proto__ === Class.prototype?
78+
...
79+
// si une réponse est vraie, renvoie true
80+
// sinon, si nous arrivons au bout de la chaîne, renvoie false
81+
```
8282
83-
In the example above `rabbit.__proto__ === Rabbit.prototype`, so that gives the answer immediately.
83+
Dans l'exemple ci-dessus, `rabbit.__ proto__ === Rabbit.prototype`, donne donc la réponse immédiatement.
8484
85-
In the case of an inheritance, the match will be at the second step:
85+
     Dans le cas d'un héritage, le match sera à la deuxième étape:
8686
87-
```js run
88-
class Animal {}
89-
class Rabbit extends Animal {}
87+
```js run
88+
class Animal {}
89+
class Rabbit extends Animal {}
9090

91-
let rabbit = new Rabbit();
92-
*!*
93-
alert(rabbit instanceof Animal); // true
94-
*/!*
91+
let rabbit = new Rabbit();
92+
*!*
93+
alert(rabbit instanceof Animal); // true
94+
*/!*
9595

96-
// rabbit.__proto__ === Rabbit.prototype
97-
*!*
98-
// rabbit.__proto__.__proto__ === Animal.prototype (match!)
99-
*/!*
100-
```
96+
// rabbit.__proto__ === Rabbit.prototype
97+
*!*
98+
// rabbit.__proto__.__proto__ === Animal.prototype (match!)
99+
*/!*
100+
```
101101
102-
Here's the illustration of what `rabbit instanceof Animal` compares with `Animal.prototype`:
102+
Voici l'illustration de ce que `rabbit instanceof Animal` compare avec `Animal.prototype`:
103103
104104
![](instanceof.svg)
105105
106-
By the way, there's also a method [objA.isPrototypeOf(objB)](mdn:js/object/isPrototypeOf), that returns `true` if `objA` is somewhere in the chain of prototypes for `objB`. So the test of `obj instanceof Class` can be rephrased as `Class.prototype.isPrototypeOf(obj)`.
106+
À propos, il y a aussi une méthode [objA.isPrototypeOf(objB)](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Object/isPrototypeOf), qui renvoie `true` si `objA` se trouve quelque part dans la chaîne de prototypes pour `objB`. Ainsi, le test de `obj instanceof Class` peut être reformulé comme suit: `Class.prototype.isPrototypeOf(obj) `.
107107
108-
That's funny, but the `Class` constructor itself does not participate in the check! Only the chain of prototypes and `Class.prototype` matters.
108+
C'est drôle, mais le constructeur `Class` lui-même ne participe pas au contrôle! Seule la chaîne de prototypes et `Class.prototype` compte.
109109
110-
That can lead to interesting consequences when `prototype` property is changed after the object is created.
110+
Cela peut avoir des conséquences intéressantes lorsque la propriété `prototype` est modifiée après la création de l'objet.
111111
112-
Like here:
112+
Comme ici:
113113
114114
```js run
115115
function Rabbit() {}
116116
let rabbit = new Rabbit();
117117

118-
// changed the prototype
118+
// le prototype est changé
119119
Rabbit.prototype = {};
120120

121-
// ...not a rabbit any more!
121+
// ...plus un rabbit!
122122
*!*
123123
alert( rabbit instanceof Rabbit ); // false
124124
*/!*
125125
```
126126
127-
## Bonus: Object.prototype.toString for the type
127+
## Bonus: Object.prototype.toString pour le type
128128
129-
We already know that plain objects are converted to string as `[object Object]`:
129+
Nous savons déjà que les objets simples sont convertis en chaîne sous la forme `[objet Objet]`:
130130
131131
```js run
132132
let obj = {};
133133

134134
alert(obj); // [object Object]
135-
alert(obj.toString()); // the same
135+
alert(obj.toString()); // la même chose
136136
```
137137
138-
That's their implementation of `toString`. But there's a hidden feature that makes `toString` actually much more powerful than that. We can use it as an extended `typeof` and an alternative for `instanceof`.
138+
C'est leur implémentation de `toString`. Mais il existe une fonctionnalité cachée qui rend `toString` beaucoup plus puissant que cela. Nous pouvons l'utiliser comme un `typeof` étendu et une alternative pour `instanceof`.
139139
140-
Sounds strange? Indeed. Let's demystify.
140+
Cela semble étrange? Effectivement. Démystifions.
141141
142-
By [specification](https://tc39.github.io/ecma262/#sec-object.prototype.tostring), the built-in `toString` can be extracted from the object and executed in the context of any other value. And its result depends on that value.
142+
Par [spécification](https://tc39.github.io/ecma262/#sec-object.prototype.tostring), le `toString` intégré peut être extrait de l'objet et exécuté dans le contexte de toute autre valeur. Et son résultat dépend de cette valeur.
143143
144-
- For a number, it will be `[object Number]`
145-
- For a boolean, it will be `[object Boolean]`
146-
- For `null`: `[object Null]`
147-
- For `undefined`: `[object Undefined]`
148-
- For arrays: `[object Array]`
149-
- ...etc (customizable).
144+
- Pour un nombre, ce sera `[object Number]`
145+
- Pour un booléen, ce sera `[object Boolean]`
146+
- Pour `null`: `[objet Null]`
147+
- Pour `undefined`: `[objet Undefined]`
148+
- Pour les tableaux: `[objet Array]`
149+
- ... etc (personnalisable).
150150
151-
Let's demonstrate:
151+
Montrons cela:
152152
153153
```js run
154-
// copy toString method into a variable for convenience
154+
// copier la méthode toString dans une variable pour plus d'utilité
155155
let objectToString = Object.prototype.toString;
156156

157-
// what type is this?
157+
// quel type est-ce?
158158
let arr = [];
159159

160160
alert( objectToString.call(arr) ); // [object *!*Array*/!*]
161161
```
162162
163-
Here we used [call](mdn:js/function/call) as described in the chapter [](info:call-apply-decorators) to execute the function `objectToString` in the context `this=arr`.
163+
Ici nous avons utilisé [call](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Function/call) comme décrit dans le chapitre [](info:call-apply-decorators) exécuter la fonction `objectToString` dans le contexte `this=arr`.
164164
165-
Internally, the `toString` algorithm examines `this` and returns the corresponding result. More examples:
165+
En interne, l'algorithme `toString` examine `this` et renvoie le résultat correspondant. Plus d'exemples:
166166
167167
```js run
168168
let s = Object.prototype.toString;
@@ -174,9 +174,9 @@ alert( s.call(alert) ); // [object Function]
174174
175175
### Symbol.toStringTag
176176
177-
The behavior of Object `toString` can be customized using a special object property `Symbol.toStringTag`.
177+
Le comportement de Object `toString` peut être personnalisé à l'aide d'une propriété d'objet spéciale `Symbol.toStringTag`.
178178
179-
For instance:
179+
Par exemple:
180180
181181
```js run
182182
let user = {
@@ -186,33 +186,33 @@ let user = {
186186
alert( {}.toString.call(user) ); // [object User]
187187
```
188188
189-
For most environment-specific objects, there is such a property. Here are few browser specific examples:
189+
Pour la plupart des objets spécifiques à l'environnement, il existe une telle propriété. Voici quelques exemples spécifiques à votre navigateur:
190190
191191
```js run
192-
// toStringTag for the environment-specific object and class:
192+
// toStringTag pour l'objet et la classe spécifiques à l'environnement:
193193
alert( window[Symbol.toStringTag]); // window
194194
alert( XMLHttpRequest.prototype[Symbol.toStringTag] ); // XMLHttpRequest
195195

196196
alert( {}.toString.call(window) ); // [object Window]
197197
alert( {}.toString.call(new XMLHttpRequest()) ); // [object XMLHttpRequest]
198198
```
199199
200-
As you can see, the result is exactly `Symbol.toStringTag` (if exists), wrapped into `[object ...]`.
200+
Comme vous pouvez le constater, le résultat est exactement `Symbol.toStringTag` (s'il existe), encapsulé dans `[objet ...]`.
201201
202-
At the end we have "typeof on steroids" that not only works for primitive data types, but also for built-in objects and even can be customized.
202+
À la fin, nous avons "typeof sur stéroïdes" qui fonctionne non seulement pour les types de données primitifs, mais aussi pour les objets intégrés et peut même être personnalisé.
203203
204-
We can use `{}.toString.call` instead of `instanceof` for built-in objects when we want to get the type as a string rather than just to check.
204+
Nous pouvons utiliser `{}.toString.call` au lieu de `instanceof` pour les objets intégrés lorsque nous voulons obtenir le type sous forme de chaîne plutôt que simplement pour vérifier.
205205
206-
## Summary
206+
## Résumé
207207
208-
Let's summarize the type-checking methods that we know:
208+
Résumons les méthodes de vérification de type que nous connaissons:
209209
210-
| | works for | returns |
210+
| | fonctionne pour | renvoie |
211211
|---------------|-------------|---------------|
212212
| `typeof` | primitives | string |
213-
| `{}.toString` | primitives, built-in objects, objects with `Symbol.toStringTag` | string |
213+
| `{}.toString` | primitives, objets intégrés, objets avec `Symbol.toStringTag` | string |
214214
| `instanceof` | objects | true/false |
215215
216-
As we can see, `{}.toString` is technically a "more advanced" `typeof`.
216+
Comme on peut le constater, `{}.toString` est techniquement un `typeof` "plus avancé".
217217
218-
And `instanceof` operator really shines when we are working with a class hierarchy and want to check for the class taking into account inheritance.
218+
Et l'opérateur `instanceof` excelle lorsque nous travaillons avec une hiérarchie de classes et voulons vérifier si la classe prend en compte l'héritage.

0 commit comments

Comments
 (0)