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'assegnazione a `Rabbit.prototype`imposta `[[Prototype]]`per i nuovi oggetti, ma non influenza gli oggetti già esistenti.
7
7
8
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 referenced both by `Rabbit.prototype`and by the `[[Prototype]]`of`rabbit`.
10
+
Gli oggetti vengono assegnati per riferimento. L'oggetto in `Rabbit.prototype`non viene duplicato, è sempre un oggetto riferito sia da `Rabbit.prototype`che da `[[Prototype]]`di`rabbit`.
11
11
12
-
So when we change its content through one reference, it is visible through the other one.
12
+
Quindi quando cambiamo il suo contenuto tramite un riferimento, questo sarà visibile anche attraverso l'altro.
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
+
Tutte le operazion di `delete`vengono applicate direttamente all'oggetto. Qui`delete rabbit.eats`prova a rimuovere la proprietà `eats`da `rabbit`, ma non esiste. Quindi l'operazione non avrà alcun effetto.
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 proprietà`eats`viene rimossa da prototype, non esiste più.
We can use such approach if we are sure that `"constructor"`property has the correct value.
1
+
Possiamo utilizzare questo approccio se siamo sicuri che il `"constructor"`possiede il valore corretto.
2
2
3
-
For instance, if we don't touch the default `"prototype"`, then this code works for sure:
3
+
Ad esempio, se non tocchiamo il `"prototype"` di default, allora il codice funzionerà di sicuro:
4
4
5
5
```js run
6
6
functionUser(name) {
@@ -10,14 +10,14 @@ function User(name) {
10
10
let user =newUser('John');
11
11
let user2 =newuser.constructor('Pete');
12
12
13
-
alert( user2.name ); // Pete (worked!)
13
+
alert( user2.name ); // Pete (ha funzionato!)
14
14
```
15
15
16
-
It worked, because`User.prototype.constructor == User`.
16
+
Ha funzionato, poiché`User.prototype.constructor == User`.
17
17
18
-
..But if someone, so to speak, overwrites`User.prototype`and forgets to recreate `constructor`to reference `User`, then it would fail.
18
+
..Ma se qualcuno, per un qualsiasi motivo, sovrascrivesse`User.prototype`e dimenticasse di ricreare il `constructor`di riferimento a `User`, allora fallirebbe.
19
19
20
-
For instance:
20
+
Ad esempio:
21
21
22
22
```js run
23
23
functionUser(name) {
@@ -33,12 +33,12 @@ let user2 = new user.constructor('Pete');
33
33
alert( user2.name ); // undefined
34
34
```
35
35
36
-
Why`user2.name`is`undefined`?
36
+
Perché`user2.name`è`undefined`?
37
37
38
-
Here's how`new user.constructor('Pete')`works:
38
+
Ecco come`new user.constructor('Pete')`funziona:
39
39
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.Prima, controlla se esiste`constructor` in `user`. Niente.
41
+
2.Successivamente segue la catena di prototype. Il prototype di`user`è`User.prototype`, e anche qui non c'è nulla.
42
+
3.Il valore di`User.prototype`è un oggetto semplice`{}`, il suo prototype è`Object.prototype`. E c'è un`Object.prototype.constructor == Object`. Quindi verrà utilizzato.
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, similar to `let user2 = {}`, that's what we have in `user2`after all.
44
+
In conclusione, abbiamo `let user2 = new Object('Pete')`. Il costruttore integrato di `Object`ignora gli argomenti, crea sempre un oggetto vuoto, in maniera simile a `let user2 = {}`, questo è ciò che abbiamo in `user2`alla fine di tutto.
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
Display the source diff
Display the rich diff
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
+
# Crea un oggetto con lo stesso costruttore
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
+
Immagina di avere un oggetto arbitrario`obj`, creato da un costruttore -- non sappiamo quale, ma vorremmo poter creare un nuovo oggetto utilizzandolo.
8
8
9
-
Can we do it like that?
9
+
Possiamo farlo in questo modo?
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
+
Fornite un esempio di costruttore per `obj`che permetta a questo codice di funzionare correttamente. Ed un esempio che non lo farebbe funzionare.
Remember, new objects can be created with a constructor function, like`new F()`.
3
+
Ricordate, nuovi oggetti possono essere creati con un costruttore, come`new F()`.
4
4
5
-
If`F.prototype`is an object, then the `new`operator uses it to set`[[Prototype]]`for the new object.
5
+
Se`F.prototype`è un oggetto, l'operatore `new`si prenderà cura di impostare`[[Prototype]]`per il nuovo oggetto.
6
6
7
7
```smart
8
-
JavaScript had prototypal inheritance from the beginning. It was one of the core features of the language.
8
+
JavaScript supporta la prototypal inheritance fin dall'inizio. Fu una delle caratteristiche principali del linguaggio.
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
+
Ma all'inizio non c'era un accesso diretto. L'unica cosa su cui ci si poteva affidare era la proprietà `"prototype"` del costruttore, descritta in questo capitolo. Per questo, esistono ancora molti script che ne fanno utilizzo.
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
+
Da notare che qui `F.prototype`, sta per una comune proprietà chiamata `"prototype"`in`F`. Sembra molto simile al termine "prototype", ma in questo caso intendiamo realmente riferirci ad una proprietà con questo nome.
14
14
15
-
Here's the example:
15
+
Vediamo qui un esempio:
16
16
17
17
```js run
18
18
let animal = {
@@ -32,27 +32,27 @@ 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
+
Impostare`Rabbit.prototype = animal`fa letteralmente quanto segue: "Quando un nuovo `new Rabbit`viene creato, assegna il suo `[[Prototype]]`ad`animal`".
36
36
37
-
That's the resulting picture:
37
+
Questo è il risultato:
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
+
In figura, `"prototype"`è una freccia orrizzontale, ciò significa che è una comune proprietà, mentre`[[Prototype]]`è verticale, quindi `rabbit`eredita da`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.
43
+
```smart header="`F.prototype`viene utilizzato solamente al momento in cui si invoca `new F`"
44
+
`F.prototype`viene utilizzata solamente quando si invoca `new F`, e si occupa di assegnare `[[Prototype]]`del nuovo oggetto.
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
+
Se, dopo la creazione, `F.prototype`cambia (`F.prototype = <another object>`), allora verrà creato un nuovo oggetto con `new F`che avrà un altro oggetto come `[[Prototype]]`, ma gli oggetti già esistenti faranno riferimento a quello vecchio.
47
47
```
48
48
49
-
## Default F.prototype, constructor property
49
+
## Default F.prototype, la proprietà constructor
50
50
51
-
Every function has the `"prototype"` property even if we don't supply it.
51
+
Ogni funzione possiede la proprietà `"prototype"` anche se non gliela forniamo direttamente.
52
52
53
-
The default `"prototype"` is an object with the only property `constructor` that points back to the function itself.
53
+
Il `"prototype"` di default è un oggetto con un'unica proprietà, il `constructor` che punta alla funzione stessa.
We can use`constructor`property to create a new object using the same constructor as the existing one.
91
+
Possiamo utilizzare il`constructor`per creare un nuovo oggetto utilizzando lo stesso costruttore dell'oggetto già esistente.
92
92
93
-
Like here:
93
+
Come nell'esempio:
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
+
Questo torna molto utile quando abbiamo un oggetto, ma non sappiamo quale costruttore è stato utilizzato (ad esempio se arriva da una libreria di terze parti), e abbiamo bisogno di crearne un altro dello stesso tipo.
109
109
110
-
But probably the most important thing about`"constructor"`is that...
110
+
Ma probabilmente la cosa più importante del`"constructor"`è che...
111
111
112
-
**...JavaScript itself does not ensure the right `"constructor"` value.**
112
+
**...JavaScript stesso non garantisce il giusto valore del `"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
+
E' vero, esiste di default nel `"prototype"`delle funzioni, ma questo è tutto. Ciò che accade dopo -- è solo nostra responsabilità.
115
115
116
-
In particular, if we replace the default prototype as a whole, then there will be no`"constructor"` in it.
116
+
In particolare, se rimpiazziamo completamente il prototype di default, allora non ci sarà alcun`"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
+
Quindi, per mantenere il `"constructor"`corretto, possiamo decidere di aggiungere/rimuovere proprietà al `"prototype"`di default, invece che sovrascriverlo completamente:
133
133
134
134
```js
135
135
functionRabbit() {}
136
136
137
-
//Not overwrite Rabbit.prototype totally
138
-
//just add to it
137
+
//Non sovrascriviamo Rabbit.prototype completamente
138
+
//aggiungiamo semplicemente una proprietà
139
139
Rabbit.prototype.jumps=true
140
-
//the default Rabbit.prototype.constructor is preserved
140
+
//il Rabbit.prototype.constructor viene cosi preservato
141
141
```
142
142
143
-
Or, alternatively, recreate the `constructor`property manually:
143
+
O, in alternativa, possiamo ricreare il `constructor`manualmente:
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
+
//ora il costruttore è corretto, perché lo abbiamo aggiunto noi
154
154
```
155
155
156
156
157
-
## Summary
157
+
## Riepilogo
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
+
In questo capitolo abbiamo descritto brevemente il modo in cui impostare il `[[Prototype]]` per gli oggetti generati tramite il costruttore. Più avanti vedremo dei pattern più avanzati su cui fare affidamento.
160
160
161
-
Everything is quite simple, just a few notes to make things clear:
161
+
Il tutto è abbastanza semplice, solo alcune note per renderlo più chiaro:
162
162
163
-
- The `F.prototype` property (don't mistake it for `[[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 proprietà `F.prototype` (danonconfonderecon`[[Prototype]]`) imposta `[[Prototype]]` dei nuovi oggetti quando viene invocato `new F()`.
164
+
- Il valore di `F.prototype` può essere sia un oggetto che `null`: altri valori verranno ignorati.
165
+
- La proprietà `"prototype"` ha un effetto speciale quando impostata in un costruttore, ed invocata con `new`.
166
166
167
-
On regular objects the `prototype` is nothing special:
167
+
Negli oggetti "comuni" la proprietà `prototype` non ha alcun significato speciale:
168
168
```js
169
169
let user = {
170
170
name:"John",
171
-
prototype: "Bla-bla" //no magic at all
171
+
prototype:"Bla-bla"//nessuna magia
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
+
Di default tutte le funzioni hanno `F.prototype= { constructor: F }`, quindi possiamo ottenere il costruttore di un oggetto accedendo alla sua proprietà `"constructor"`.
0 commit comments