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
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.
7
7
8
-
2.`false`.
8
+
2.`false`.
9
9
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`.
11
11
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.
13
13
14
14
3.`true`.
15
15
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.
17
17
18
18
4.`undefined`.
19
19
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.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é.
43
43
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.
Copy file name to clipboardExpand all lines: 1-js/08-prototypes/02-function-prototype/4-new-object-same-constructor/task.md
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,14 @@ importance: 5
2
2
3
3
---
4
4
5
-
# Create an object with the same constructor
5
+
# Créer un objet avec le même constructeur
6
6
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.
8
8
9
-
Can we do it like that?
9
+
Pouvons-nous le faire comme ça?
10
10
11
11
```js
12
12
let obj2 =newobj.constructor();
13
13
```
14
14
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.
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()`.
4
4
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.
6
6
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.
9
9
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.
11
11
```
12
12
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.
14
14
15
-
Here's the example:
15
+
Voici l'exemple:
16
16
17
17
```js run
18
18
let animal = {
@@ -32,65 +32,65 @@ let rabbit = new Rabbit("White Rabbit"); // rabbit.__proto__ == animal
32
32
alert( rabbit.eats ); // true
33
33
```
34
34
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`".
36
36
37
-
That's the resulting picture:
37
+
C'est l'image résultante:
38
38
39
39

40
40
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`.
42
42
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".
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".
45
45
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.
47
47
```
48
48
49
-
## Default F.prototype, constructor property
49
+
## F.prototype par défaut, propriété du constructeur
50
50
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.
52
52
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.
alert(rabbit.constructor== Rabbit); // true (de prototype)
87
87
```
88
88
89
89

90
90
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.
92
92
93
-
Like here:
93
+
Comme ici:
94
94
95
95
```js run
96
96
functionRabbit(name) {
@@ -105,17 +105,17 @@ let rabbit2 = new rabbit.constructor("Black Rabbit");
105
105
*/!*
106
106
```
107
107
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.
109
109
110
-
But probably the most important thing about `"constructor"`is that...
110
+
Mais probablement la chose la plus importante à propos de `"constructor"`est que...
111
111
112
-
**...JavaScript itself does not ensure the right `"constructor"` value.**
112
+
**...JavaScript lui-même n'assure pas la bonne valeur de `"constructor"`.**
113
113
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.
115
115
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".
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:
133
133
134
134
```js
135
135
functionRabbit() {}
136
136
137
-
//Not overwrite Rabbit.prototype totally
138
-
//just add to it
137
+
//Ne pas écraser Rabbit.prototype totalement
138
+
//juste y ajouter
139
139
Rabbit.prototype.jumps=true
140
-
//the default Rabbit.prototype.constructor is preserved
140
+
//le Rabbit.prototype.constructor par défaut est conservé
141
141
```
142
142
143
-
Or, alternatively, recreate the `constructor` property manually:
143
+
Ou bien, recréez manuellement la propriété `constructor`:
144
144
145
145
```js
146
146
Rabbit.prototype= {
@@ -150,26 +150,26 @@ Rabbit.prototype = {
150
150
*/!*
151
151
};
152
152
153
-
//now constructor is also correct, because we added it
153
+
//maintenant le constructeur est également correct, car nous l'avons ajouté
154
154
```
155
155
156
156
157
-
## Summary
157
+
## Résumé
158
158
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.
160
160
161
-
Everything is quite simple, just few notes to make things clear:
161
+
Tout est assez simple, juste quelques précisions:
162
162
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'twork.
165
-
-The`"prototype"`propertyonlyhassuchaspecialeffectwhensetonaconstructor function, and invoked with `new`.
163
+
- La propriété `F.prototype` (neplaisantepasavec`[[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`.
166
166
167
-
On regular objects the `prototype` is nothing special:
167
+
Sur les objets ordinaires, le `prototype` n'a rien de spécial:
168
168
```js
169
169
let user = {
170
170
name:"John",
171
-
prototype: "Bla-bla" //no magic at all
171
+
prototype:"Bla-bla"//pas de magie
172
172
};
173
173
```
174
174
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