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
First, let's see why the latter code doesn't work.
1
+
Come prima cosa, cerchiamo di capire perché il codice non funziona.
2
2
3
-
The reason becomes obvious if we try to run it. An inheriting class constructor must call`super()`. Otherwise `"this"`won't be "defined".
3
+
La motivazione appare piuttosto ovvia se proviamo ad eseguire il codice. Un classe che eredita, deve invocare`super()`. Diversamente, il valore di `"this"`non sarà "definito".
4
4
5
-
So here's the fix:
5
+
Vediamo come sistemarlo:
6
6
7
7
```js run
8
8
classRabbitextendsObject {
9
9
constructor(name) {
10
10
*!*
11
-
super(); //need to call the parent constructor when inheriting
11
+
super(); //dobbiamo chiamare il costruttore padre della classe da cui stiamo ereditando
12
12
*/!*
13
13
this.name= name;
14
14
}
@@ -19,16 +19,16 @@ let rabbit = new Rabbit("Rab");
19
19
alert( rabbit.hasOwnProperty('name') ); // true
20
20
```
21
21
22
-
But that's not all yet.
22
+
Ma non è tutto.
23
23
24
-
Even after the fix, there's still important difference in `"class Rabbit extends Object"`versus`class Rabbit`.
24
+
Anche dopo questo fix, c'è ancora un grande differenza tra `"class Rabbit extends Object"`e`class Rabbit`.
25
25
26
-
As we know, the "extends" syntax sets up two prototypes:
26
+
Come già sappiamo, la sintassi "extends" imposta due prototype:
27
27
28
-
1.Between`"prototype"`of the constructor functions (for methods).
29
-
2.Between the constructor functions themselves (for static methods).
28
+
1.Tra`"prototype"`del costruttore (per i metodi).
29
+
2.Tra i costruttori stessi (per i metodi statici).
30
30
31
-
In our case, for `class Rabbit extends Object`it means:
31
+
Nel nostro caso, `class Rabbit extends Object`significa:
So`Rabbit` doesn't provide access to static methods of `Object` in that case.
68
+
Quindi`Rabbit`, in questo caso, non fornisce l'accesso ai metodi statici di `Object`.
69
69
70
-
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`.
70
+
In ogni caso, `Function.prototype`possiede metodi "generici", come`call`, `bind` etc. Questi saranno disponibili in entrambi i casi, grazie al costruttore di`Object`, `Object.__proto__ === Function.prototype`.
71
71
72
-
Here's the picture:
72
+
Come mostrato in figura:
73
73
74
74

75
75
76
-
So, to put it short, there are two differences:
76
+
Quindi, per riassumere, ci sono due principali differenze:
Copy file name to clipboardExpand all lines: 1-js/09-classes/03-static-properties-methods/3-class-extend-object/task.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@ importance: 3
4
4
5
5
# Class extends Object?
6
6
7
-
As we know, all objects normally inherit from `Object.prototype`and get access to "generic" object methods like`hasOwnProperty` etc.
7
+
Come ormai sappiamo, tutti gli oggetti, normalmente ereditano da `Object.prototype`ed hanno accesso ai metodi generici di `Object`, come`hasOwnProperty` etc.
8
8
9
-
For instance:
9
+
Ad esempio:
10
10
11
11
```js run
12
12
classRabbit {
@@ -18,16 +18,16 @@ class Rabbit {
18
18
let rabbit =newRabbit("Rab");
19
19
20
20
*!*
21
-
// hasOwnProperty method is from Object.prototype
21
+
// hasOwnProperty viene ereditato da Object.prototype
22
22
alert( rabbit.hasOwnProperty('name') ); // true
23
23
*/!*
24
24
```
25
25
26
-
But if we spell it out explicitly like`"class Rabbit extends Object"`, then the result would be different from a simple`"class Rabbit"`?
26
+
Ma se lo invocassimo esplicitamente in questo modo:`"class Rabbit extends Object"`, allora il risultato sarebbe diverso da un semplice`"class Rabbit"`?
27
27
28
-
What's the difference?
28
+
Qual'è la differenza?
29
29
30
-
Here's an example of such code (it doesn't work -- why? fix it?):
30
+
Qui vediamo un esempio (perché non funziona? è possibile sistemarlo?):
The value of`this`in `User.staticMethod()`call is the class constructor `User`itself (the "object before dot" rule).
32
+
Il valore di`this`nella chiamata `User.staticMethod()`è rappresentato dal costruttore dell classe `User`(la regola dell' "oggetto prima del punto").
33
33
34
-
Usually, static methods are used to implement functions that belong to the class, but not to any particular object of it.
34
+
Solitamente, i metodi statici vengono utilizzati per rappresentare funzioni che appartengono alla classe, ma non ad un oggetto in particolare.
35
35
36
-
For instance, we have `Article`objects and need a function to compare them. A natural solution would be to add `Article.compare` method, like this:
36
+
Ad esempio, potremmo avere degli oggetti di tipo `Article`e necessitare di una funzione per confrontarli. Una soluzione naturale sarebbe quella di aggiungere il metodo `Article.compare`, come nell'esempio:
Here`Article.compare`stands "above" articles, as a means to compare them. It's not a method of an article, but rather of the whole class.
66
+
Qui`Article.compare`sta "al di sopra" degli articoli, poiché ha lo scopo di confrontarli. Non è un metodo di un articolo, ma piuttosto dell'intera classe.
67
67
68
-
Another example would be a so-called "factory" method. Imagine, we need few ways to create an article:
68
+
Un altro esempio comune è quello del "factory method" (un particolare design pattern). Immaginiamo di avere bisogno di diverse modalità di creazione di un articolo:
69
69
70
-
1.Create by given parameters (`title`, `date` etc).
71
-
2.Create an empty article with today's date.
72
-
3. ...or else somehow.
70
+
1.Creazione con i parametri forniti (`title`, `date` etc).
71
+
2.Creazione di un articolo vuoto con la data di oggi.
72
+
3. ...o qualsiasi altra modalità.
73
73
74
-
The first way can be implemented by the constructor. And for the second one we can make a static method of the class.
74
+
Il primo metodo può essere implementato tramite il costruttore. Mentre per il secondo, possiamo creare un metodo statico appartenente alla classe.
75
75
76
-
Like`Article.createTodays()`here:
76
+
Come`Article.createTodays()`nell'esempio:
77
77
78
78
```js run
79
79
classArticle {
@@ -84,7 +84,7 @@ class Article {
84
84
85
85
*!*
86
86
staticcreateTodays() {
87
-
//remember, this = Article
87
+
//ricorda, this = Article
88
88
returnnewthis("Today's digest", newDate());
89
89
}
90
90
*/!*
@@ -95,21 +95,21 @@ let article = Article.createTodays();
95
95
alert( article.title ); // Today's digest
96
96
```
97
97
98
-
Now every time we need to create a today's digest, we can call `Article.createTodays()`. Once again, that's not a method of an article, but a method of the whole class.
98
+
Ora, ogni volta in cui avremo bisogno di crare un "today's digest", possiamo invocare `Article.createTodays()`. Ripetiamolo nuovamente, questo non è un metodo per uno specifico articolo, ma piuttosto un metodo dell'intera classe.
99
99
100
-
Static methods are also used in database-related classes to search/save/remove entries from the database, like this:
100
+
I metodi statici vengono utilizzati anche nelle classi database-related (relative a database), per poter cercare/salvare/rimuovere elementi dal database, come nell'esempio:
101
101
102
102
```js
103
-
//assuming Article is a special class for managing articles
104
-
//static method to remove the article:
103
+
//assumiamo che Article sia una classe speciale per la gestione degli articoli
104
+
//metodo statico per la rimozione di un articolo:
105
105
Article.remove({id:12345});
106
106
```
107
107
108
-
## Static properties
108
+
## Proprietà statiche
109
109
110
110
[recent browser=Chrome]
111
111
112
-
Static properties are also possible, they look like regular class properties, but prepended by`static`:
112
+
E' anche possibile definire proprietà statiche, queste sono molto simili alle proprietà della classe, ma sono precedute dalla keyword`static`:
113
113
114
114
```js run
115
115
classArticle {
@@ -119,17 +119,17 @@ class Article {
119
119
alert( Article.publisher ); // Ilya Kantor
120
120
```
121
121
122
-
That is the same as a direct assignment to`Article`:
122
+
Lo stesso che si otterrebbe con un assegnazione diretta ad`Article`:
123
123
124
124
```js
125
125
Article.publisher="Ilya Kantor";
126
126
```
127
127
128
-
## Inheritance of static properties and methods
128
+
## Ereditarietà delle proprietà e dei metodi statici
129
129
130
-
Static properties and methods are inherited.
130
+
Anche le proprietà ed i metodi statici vengono ereditati.
131
131
132
-
For instance, `Animal.compare`and`Animal.planet`in the code below are inherited and accessible as `Rabbit.compare`and`Rabbit.planet`:
132
+
Ad esempio, `Animal.compare`e`Animal.planet`nel codice sotto, vengono ereditate e diventano quindi accessibili come `Rabbit.compare`e`Rabbit.planet`:
133
133
134
134
```js run
135
135
classAnimal {
@@ -153,7 +153,7 @@ class Animal {
153
153
154
154
}
155
155
156
-
//Inherit from Animal
156
+
//Eredita da Animal
157
157
classRabbitextendsAnimal {
158
158
hide() {
159
159
alert(`${this.name} hides!`);
@@ -174,43 +174,43 @@ rabbits[0].run(); // Black Rabbit runs with speed 5.
174
174
alert(Rabbit.planet); // Earth
175
175
```
176
176
177
-
Now when we call `Rabbit.compare`, the inherited `Animal.compare`will be called.
177
+
Ora, quando invochiamo `Rabbit.compare`, verrà invocato il metodo `Animal.compare`ereditato.
178
178
179
-
How does it work? Again, using prototypes. As you might have already guessed, `extends`gives `Rabbit`the `[[Prototype]]`reference to`Animal`.
179
+
Come funziona? Nuovamente, utilizzando il prototypes. Come potrete aver già intuito, `extends`fornisce a `Rabbit`il riferimento a `[[Prototype]]`di`Animal`.
Static methods are used for the functionality that belongs to the class "as a whole". It doesn't relate to a concrete class instance.
205
+
I metodi statici vengono utilizzati per funzionalità che appartengono all'intera classe. Non hanno nulla a che fare con l'istanza della classe.
206
206
207
-
For example, a method for comparison `Article.compare(article1, article2)`or a factory method `Article.createTodays()`.
207
+
Ad esempio, un metodo per il confronto `Article.compare(article1, article2)`o un factory method `Article.createTodays()`.
208
208
209
-
They are labeled by the word `static`in class declaration.
209
+
Queste vengono precedute dalla keyword `static`all'interno della dichiarazione della classe.
210
210
211
-
Static properties are used when we'd like to store class-level data, also not bound to an instance.
211
+
Le proprietà statiche vengono utilizzate quando si ha intenzione di memorizzare dati relativi alla classe, che non sono quindi legati ad un'istanza precisa.
212
212
213
-
The syntax is:
213
+
La sintassi è:
214
214
215
215
```js
216
216
classMyClass {
@@ -222,13 +222,13 @@ class MyClass {
222
222
}
223
223
```
224
224
225
-
Technically, static declaration is the same as assigning to the class itself:
225
+
Tecnicamente, le dichiarazioni di proprietà statiche equivalgono all'assegnazione diretta alla classe stessa:
226
226
227
227
```js
228
228
MyClass.property=...
229
229
MyClass.method=...
230
230
```
231
231
232
-
Static properties and methods are inherited.
232
+
Le proprietà ed i metodi statici vengono ereditati.
233
233
234
-
For `class B extends A`the prototype of the class `B`itself points to `A`: `B.[[Prototype]] = A`. So if a field is not found in `B`, the search continues in `A`.
234
+
Nel caso in cui `class B extends A`il prototype della classe `B`punta ad `A`: `B.[[Prototype]] = A`. Quindi se un campo non viene trovato in `B`, la ricerca continuerà in `A`.
0 commit comments