Skip to content

Commit cbce59f

Browse files
authored
Merge pull request #67 from TevaHenry/master
F.prototype
2 parents 3108a2d + ebe5134 commit cbce59f

File tree

5 files changed

+78
-78
lines changed

5 files changed

+78
-78
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11

2-
Answers:
2+
Réponses:
33

4-
1. `true`.
4+
1. `true`.
55

6-
The assignment to `Rabbit.prototype` sets up `[[Prototype]]` for new objects, but it does not affect the existing ones.
6+
     L'affectation à `Rabbit.prototype` configure `[[Prototype]]` pour les nouveaux objets, mais n'affecte pas les objets existants.
77

8-
2. `false`.
8+
2. `false`.
99

10-
Objects are assigned by reference. The object from `Rabbit.prototype` is not duplicated, it's still a single object is referenced both by `Rabbit.prototype` and by the `[[Prototype]]` of `rabbit`.
10+
     Les objets sont assignés par référence. L'objet de `Rabbit.prototype` n'est pas dupliqué, mais un objet unique est référencé à la fois par `Rabbit.prototype` et par le `[[Prototype]]` de `rabbit`.
1111

12-
So when we change its content through one reference, it is visible through the other one.
12+
     Ainsi, lorsque nous modifions son contenu par l’une des références, il est visible par l’autre.
1313

1414
3. `true`.
1515

16-
All `delete` operations are applied directly to the object. Here `delete rabbit.eats` tries to remove `eats` property from `rabbit`, but it doesn't have it. So the operation won't have any effect.
16+
     Toutes les opérations `delete` sont appliquées directement à l'objet. `Delete rabbit.eats` tente ici de supprimer la propriété `eats` de `rabbit`, mais ne l’a pas. Donc l'opération n'aura aucun effet.
1717

1818
4. `undefined`.
1919

20-
The property `eats` is deleted from the prototype, it doesn't exist any more.
20+
     La propriété `eats` est supprimée du prototype, elle n’existe plus.

1-js/08-prototypes/02-function-prototype/1-changing-prototype/task.md

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

33
---
44

5-
# Changing "prototype"
5+
# Changement de "prototype"
66

7-
In the code below we create `new Rabbit`, and then try to modify its prototype.
7+
Dans le code ci-dessous, nous créons `new Rabbit`, puis essayons de modifier son prototype.
88

9-
In the start, we have this code:
9+
Au début, nous avons ce code:
1010

1111
```js run
1212
function Rabbit() {}
@@ -20,7 +20,7 @@ alert( rabbit.eats ); // true
2020
```
2121

2222

23-
1. We added one more string (emphasized), what `alert` shows now?
23+
1. Nous avons ajouté une chaîne supplémentaire (accentué), que montre `alert` maintenant?
2424

2525
```js
2626
function Rabbit() {}
@@ -37,7 +37,7 @@ alert( rabbit.eats ); // true
3737
alert( rabbit.eats ); // ?
3838
```
3939

40-
2. ...And if the code is like this (replaced one line)?
40+
2. ...Et si le code est comme ça (une ligne remplacé)?
4141

4242
```js
4343
function Rabbit() {}
@@ -54,7 +54,7 @@ alert( rabbit.eats ); // true
5454
alert( rabbit.eats ); // ?
5555
```
5656

57-
3. Like this (replaced one line)?
57+
3. Comme ceci (une ligne remplacé)?
5858

5959
```js
6060
function Rabbit() {}
@@ -71,7 +71,7 @@ alert( rabbit.eats ); // true
7171
alert( rabbit.eats ); // ?
7272
```
7373

74-
4. The last variant:
74+
4. La dernière variante:
7575

7676
```js
7777
function Rabbit() {}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
We can use such approach if we are sure that `"constructor"` property has the correct value.
1+
Nous pouvons utiliser cette approche si nous sommes sûrs que la propriété `"constructeur"` a la valeur correcte.
22

3-
For instance, if we don't touch the default `"prototype"`, then this code works for sure:
3+
Par exemple, si nous ne touchons pas le `"prototype"` par défaut, alors ce code fonctionne à coup sûr:
44

55
```js run
66
function User(name) {
@@ -10,14 +10,14 @@ function User(name) {
1010
let user = new User('John');
1111
let user2 = new user.constructor('Pete');
1212

13-
alert( user2.name ); // Pete (worked!)
13+
alert( user2.name ); // Pete (ça marche!)
1414
```
1515

16-
It worked, because `User.prototype.constructor == User`.
16+
Cela a fonctionné, car `User.prototype.constructor == User`.
1717

18-
..But if someone, so to say, overwrites `User.prototype` and forgets to recreate `"constructor"`, then it would fail.
18+
..Mais si quelqu'un, pour ainsi dire, écrase `User.prototype` et oublie de recréer `"constructor"`, alors cela échouera.
1919

20-
For instance:
20+
Par exemple:
2121

2222
```js run
2323
function User(name) {
@@ -33,12 +33,12 @@ let user2 = new user.constructor('Pete');
3333
alert( user2.name ); // undefined
3434
```
3535

36-
Why `user2.name` is `undefined`?
36+
Pourquoi `user2.name` est `undefined`?
3737

38-
Here's how `new user.constructor('Pete')` works:
38+
Voici comment `new user.constructor('Pete')` fonctionne:
3939

40-
1. First, it looks for `constructor` in `user`. Nothing.
41-
2. Then it follows the prototype chain. The prototype of `user` is `User.prototype`, and it also has nothing.
42-
3. The value of `User.prototype` is a plain object `{}`, its prototype is `Object.prototype`. And there is `Object.prototype.constructor == Object`. So it is used.
40+
1. Tout d'abord, il cherche `constructor` dans `user`. Rien.
41+
2. Ensuite, il suit la chaîne de prototype. Le prototype de `user` est `User.prototype` et il n'a également rien.
42+
3. La valeur de `User.prototype` est un objet simple `{}`, son prototype est `Object.prototype`. Et il y a `Object.prototype.constructor == Object`. Alors c'est utilisé.
4343

44-
At the end, we have `let user2 = new Object('Pete')`. The built-in `Object` constructor ignores arguments, it always creates an empty object -- that's what we have in `user2` after all.
44+
À la fin, nous avons `let user2 = new Object('Pete')`. Le constructeur `Object` intégré ignore les arguments, il crée toujours un objet vide - c'est ce que nous avons dans `user2` après tout.

1-js/08-prototypes/02-function-prototype/4-new-object-same-constructor/task.md

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

33
---
44

5-
# Create an object with the same constructor
5+
# Créer un objet avec le même constructeur
66

7-
Imagine, we have an arbitrary object `obj`, created by a constructor function -- we don't know which one, but we'd like to create a new object using it.
7+
Imaginez nous avons un objet arbitraire `obj`, créé par une fonction constructeur - nous ne savons pas lequel, mais nous aimerions créer un nouvel objet à l'aide de celui-ci.
88

9-
Can we do it like that?
9+
Pouvons-nous le faire comme ça?
1010

1111
```js
1212
let obj2 = new obj.constructor();
1313
```
1414

15-
Give an example of a constructor function for `obj` which lets such code work right. And an example that makes it work wrong.
15+
Donne un exemple de fonction constructeur pour `obj` qui laisse ce code fonctionner correctement. Et un exemple qui fait que ça marche mal.
Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# F.prototype
22

3-
Remember, new objects can be created with a constructor function, like `new F()`.
3+
Rappelez-vous que de nouveaux objets peuvent être créés avec une fonction constructeur, comme `new F()`.
44

5-
If `F.prototype` is an object, then `new` operator uses it to set `[[Prototype]]` for the new object.
5+
Si `F.prototype` est un objet, l'opérateur `new` l'utilise pour définir `[[Prototype]]` pour le nouvel objet.
66

7-
```smart
8-
JavaScript had prototypal inheritance from the beginning. It was one of the core features of the language.
7+
```smart header="Veuillez noter"
8+
JavaScript avait l'héritage prototypique depuis le début. C'était l'une des caractéristiques principales de la langue.
99
10-
But in the old times, there was no direct access to it. The only thing that worked reliably was a `"prototype"` property of the constructor function, described in this chapter. So there are many scripts that still use it.
10+
Mais dans le passé, il n'y avait pas d'accès direct. La seule chose qui fonctionnait de manière fiable est une propriété `"prototype"` de la fonction constructeur décrite dans ce chapitre. Donc, il y a beaucoup de scripts qui l'utilisent encore.
1111
```
1212

13-
Please note that `F.prototype` here means a regular property named `"prototype"` on `F`. It sounds something similar to the term "prototype", but here we really mean a regular property with this name.
13+
Veuillez noter que `F.prototype` signifie ici une propriété régulière nommée `"prototype"` sur `F`. Cela ressemble quelque peu au terme "prototype", mais nous entendons ici une propriété régulière portant ce nom.
1414

15-
Here's the example:
15+
Voici l'exemple:
1616

1717
```js run
1818
let animal = {
@@ -32,65 +32,65 @@ let rabbit = new Rabbit("White Rabbit"); // rabbit.__proto__ == animal
3232
alert( rabbit.eats ); // true
3333
```
3434

35-
Setting `Rabbit.prototype = animal` literally states the following: "When a `new Rabbit` is created, assign its `[[Prototype]]` to `animal`".
35+
Définir `Rabbit.prototype=animal` énonce littéralement ce qui suit: "Lorsqu'un `new Rabbit` est créé, assigner son `[[Prototype]]` à `animal`".
3636

37-
That's the resulting picture:
37+
C'est l'image résultante:
3838

3939
![](proto-constructor-animal-rabbit.svg)
4040

41-
On the picture, `"prototype"` is a horizontal arrow, meaning a regular property, and `[[Prototype]]` is vertical, meaning the inheritance of `rabbit` from `animal`.
41+
Sur l'image, `"prototype"` est une flèche horizontale, ce qui signifie une propriété normale, et `[[Prototype]]` est vertical, ce qui signifie l'héritage de `rabbit` de `animal`.
4242

43-
```smart header="`F.prototype` only used at `new F` time"
44-
`F.prototype` property is only used when `new F` is called, it assigns `[[Prototype]]` of the new object. After that, there's no connection between `F.prototype` and the new object. Think of it as a "one-time gift".
43+
```smart header="`F.prototype` utilisé uniquement pendant `new F`"
44+
La propriété `F.prototype` est utilisée uniquement lorsque `new F` est appelé, elle attribue `[[Prototype]]` du nouvel objet. Après cela, il n'y a plus de connexion entre `F.prototype` et le nouvel objet. Pensez-y comme à un "don unique".
4545

46-
If, after the creation, `F.prototype` property changes (`F.prototype = <another object>`), then new objects created by `new F` will have another object as `[[Prototype]]`, but already existing objects keep the old one.
46+
Si, après la création, la propriété `F.prototype` change (`F.prototype = <un autre objet>`), les nouveaux objets créés par `new F` auront un autre objet comme `[[Prototype]]`, mais les objets déjà existants conservent l'ancien.
4747
```
4848
49-
## Default F.prototype, constructor property
49+
## F.prototype par défaut, propriété du constructeur
5050
51-
Every function has the `"prototype"` property even if we don't supply it.
51+
Chaque fonction a la propriété `"prototype "` même si nous ne la fournissons pas.
5252
53-
The default `"prototype"` is an object with the only property `constructor` that points back to the function itself.
53+
Le `"prototype"` par défaut est un objet avec comme seule propriété `constructor` qui renvoie à la fonction elle-même.
5454
55-
Like this:
55+
Comme ça:
5656
5757
```js
5858
function Rabbit() {}
5959
60-
/* default prototype
60+
/* prototype par défaut
6161
Rabbit.prototype = { constructor: Rabbit };
6262
*/
6363
```
6464

6565
![](function-prototype-constructor.svg)
6666

67-
We can check it:
67+
Nous pouvons le vérifier:
6868

6969
```js run
7070
function Rabbit() {}
71-
// by default:
71+
// par défaut:
7272
// Rabbit.prototype = { constructor: Rabbit }
7373

7474
alert( Rabbit.prototype.constructor == Rabbit ); // true
7575
```
7676

77-
Naturally, if we do nothing, the `constructor` property is available to all rabbits through `[[Prototype]]`:
77+
Naturellement, si nous ne faisons rien, la propriété `constructor` est disponible pour tous les "rabbits" via `[[Prototype]]`:
7878

7979
```js run
8080
function Rabbit() {}
81-
// by default:
81+
// par défaut:
8282
// Rabbit.prototype = { constructor: Rabbit }
8383

84-
let rabbit = new Rabbit(); // inherits from {constructor: Rabbit}
84+
let rabbit = new Rabbit(); // hérite de {constructor: Rabbit}
8585

86-
alert(rabbit.constructor == Rabbit); // true (from prototype)
86+
alert(rabbit.constructor == Rabbit); // true (de prototype)
8787
```
8888

8989
![](rabbit-prototype-constructor.svg)
9090

91-
We can use `constructor` property to create a new object using the same constructor as the existing one.
91+
Nous pouvons utiliser la propriété `constructor` pour créer un nouvel objet en utilisant le même constructeur que l'existant.
9292

93-
Like here:
93+
Comme ici:
9494

9595
```js run
9696
function Rabbit(name) {
@@ -105,17 +105,17 @@ let rabbit2 = new rabbit.constructor("Black Rabbit");
105105
*/!*
106106
```
107107

108-
That's handy when we have an object, don't know which constructor was used for it (e.g. it comes from a 3rd party library), and we need to create another one of the same kind.
108+
C'est pratique lorsque nous avons un objet, ne sachant pas quel constructeur a été utilisé pour cela (par exemple, il provient d'une bibliothèque externe), et nous devons en créer un autre du même type.
109109

110-
But probably the most important thing about `"constructor"` is that...
110+
Mais probablement la chose la plus importante à propos de `"constructor"` est que...
111111

112-
**...JavaScript itself does not ensure the right `"constructor"` value.**
112+
**...JavaScript lui-même n'assure pas la bonne valeur de `"constructor"`.**
113113

114-
Yes, it exists in the default `"prototype"` for functions, but that's all. What happens with it later -- is totally on us.
114+
Oui, il existe dans le `"prototype"` par défaut pour les fonctions, mais c'est tout. Ce qui se passe avec apres le fait - dépend totalement de nous.
115115

116-
In particular, if we replace the default prototype as a whole, then there will be no `"constructor"` in it.
116+
En particulier, si nous remplaçons le prototype par défaut dans son ensemble, il ne contiendra pas de "constructor".
117117

118-
For instance:
118+
Par exemple:
119119

120120
```js run
121121
function Rabbit() {}
@@ -129,18 +129,18 @@ alert(rabbit.constructor === Rabbit); // false
129129
*/!*
130130
```
131131

132-
So, to keep the right `"constructor"` we can choose to add/remove properties to the default `"prototype"` instead of overwriting it as a whole:
132+
Donc, pour garder le bon `"constructor"`, nous pouvons choisir d'ajouter/supprimer des propriétés au `"prototype"` par défaut au lieu de l'écraser dans son ensemble:
133133

134134
```js
135135
function Rabbit() {}
136136

137-
// Not overwrite Rabbit.prototype totally
138-
// just add to it
137+
// Ne pas écraser Rabbit.prototype totalement
138+
// juste y ajouter
139139
Rabbit.prototype.jumps = true
140-
// the default Rabbit.prototype.constructor is preserved
140+
// le Rabbit.prototype.constructor par défaut est conservé
141141
```
142142

143-
Or, alternatively, recreate the `constructor` property manually:
143+
Ou bien, recréez manuellement la propriété `constructor`:
144144

145145
```js
146146
Rabbit.prototype = {
@@ -150,26 +150,26 @@ Rabbit.prototype = {
150150
*/!*
151151
};
152152

153-
// now constructor is also correct, because we added it
153+
// maintenant le constructeur est également correct, car nous l'avons ajouté
154154
```
155155

156156

157-
## Summary
157+
## Résumé
158158

159-
In this chapter we briefly described the way of setting a `[[Prototype]]` for objects created via a constructor function. Later we'll see more advanced programming patterns that rely on it.
159+
Dans ce chapitre, nous avons brièvement décrit la manière de définir un `[[Prototype]]` pour les objets créés via une fonction constructeur. Plus tard, nous verrons des modèles de programmation plus avancés qui en dépendent.
160160

161-
Everything is quite simple, just few notes to make things clear:
161+
Tout est assez simple, juste quelques précisions:
162162

163-
- The `F.prototype` property (don't mess with `[[Prototype]]`) sets `[[Prototype]]` of new objects when `new F()` is called.
164-
- The value of `F.prototype` should be either an object or `null`: other values won't work.
165-
- The `"prototype"` property only has such a special effect when set on a constructor function, and invoked with `new`.
163+
- La propriété `F.prototype` (ne plaisante pas avec `[[Prototype]]`) définit `[[Prototype]]` de nouveaux objets lorsque `new F()` est appelée.
164+
- La valeur de `F.prototype` devrait être un objet ou `null`: les autres valeurs ne fonctionneront pas.
165+
- La propriété `"prototype"` n'a cet effet spécial que lorsqu'elle est définie sur une fonction constructeur et invoquée avec `new`.
166166

167-
On regular objects the `prototype` is nothing special:
167+
Sur les objets ordinaires, le `prototype` n'a rien de spécial:
168168
```js
169169
let user = {
170170
name: "John",
171-
prototype: "Bla-bla" // no magic at all
171+
prototype: "Bla-bla" // pas de magie
172172
};
173173
```
174174
175-
By default all functions have `F.prototype = { constructor: F }`, so we can get the constructor of an object by accessing its `"constructor"` property.
175+
Par défaut, toutes les fonctions ont `F.prototype={constructor:F}`, nous pouvons donc obtenir le constructeur d'un objet en accédant à sa propriété `"constructor"`.

0 commit comments

Comments
 (0)