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
Built-in classes like Array, Map and others are extendable also.
4
+
Le classi built-in (integrate) come Array, Map e tutte le altre, sono anch'esse estendibili.
5
5
6
-
For instance, here `PowerArray`inherits from the native `Array`:
6
+
Ad esempio, qui vediamo `PowerArray`ereditare dall'`Array` nativo:
7
7
8
8
```js run
9
-
//add one more method to it (can do more)
9
+
//aggiungiamo un metodo (possiamo fare di più)
10
10
classPowerArrayextendsArray {
11
11
isEmpty() {
12
12
returnthis.length===0;
@@ -21,20 +21,20 @@ alert(filteredArr); // 10, 50
21
21
alert(filteredArr.isEmpty()); // false
22
22
```
23
23
24
-
Please note a very interesting thing. Built-in methods like `filter`, `map`and others -- return new objects of exactly the inherited type `PowerArray`. Their internal implementation uses the object's `constructor`property for that.
24
+
Notiamo una cosa interessante. I metodi built-in come `filter`, `map`e così via, ritornano nuovi oggetti del tipo ereditato, cioè `PowerArray`. La loro implementazione interna utilizzata la proprietà oggetto `constructor`per farlo.
25
25
26
-
In the example above,
26
+
Nell'esempio sopra,
27
27
```js
28
28
arr.constructor=== PowerArray
29
29
```
30
30
31
-
When `arr.filter()` is called, it internally creates the new array of results using exactly`arr.constructor`, not basic`Array`. That's actually very cool, because we can keep using `PowerArray`methods further on the result.
31
+
Quando invochiamo `arr.filter()`, questo creerà internamente il nuovo array contenente i risultati utilizzando`arr.constructor`, non l'oggetto`Array` standard. Questo è molto utile, poiché successivamente possiamo utilizzare i metodi di `PowerArray`sul risultato ottenuto.
32
32
33
-
Even more, we can customize that behavior.
33
+
Inoltre, possiamo personalizzarne il comportamento.
34
34
35
-
We can add a special static getter `Symbol.species`to the class. If it exists, it should return the constructor that JavaScript will use internally to create new entities in `map`, `filter`and so on.
35
+
Possiamo aggiungere uno speciale getter statico `Symbol.species`alla classe. Questo dovrebbe ritornare il costruttore che Javascript utilizzerà internamente per creare le nuove entità in `map`, `filter`e così via.
36
36
37
-
If we'd like built-in methods like `map`or`filter`to return regular arrays, we can return `Array` in `Symbol.species`, like here:
37
+
Se, ad esempio, volessimo che metodi come `map`o`filter`restituiscano un array standard, possiamo ritornare `Array` in `Symbol.species`, come nell'esempio:
38
38
39
39
```js run
40
40
classPowerArrayextendsArray {
@@ -43,7 +43,7 @@ class PowerArray extends Array {
43
43
}
44
44
45
45
*!*
46
-
// built-in methods will use this as the constructor
46
+
//i metodi built-in lo utilizzeranno come costruttore
47
47
staticget [Symbol.species]() {
48
48
returnArray;
49
49
}
@@ -53,35 +53,35 @@ class PowerArray extends Array {
53
53
let arr =newPowerArray(1, 2, 5, 10, 50);
54
54
alert(arr.isEmpty()); // false
55
55
56
-
// filter creates new array using arr.constructor[Symbol.species] as constructor
56
+
// filter crea un nuovo array utilizzando arr.constructor[Symbol.species] come costruttore
57
57
let filteredArr =arr.filter(item=> item >=10);
58
58
59
59
*!*
60
-
// filteredArr is not PowerArray, but Array
60
+
// filteredArr non è di tipo PowerArray, ma è un Array standard
61
61
*/!*
62
62
alert(filteredArr.isEmpty()); // Error: filteredArr.isEmpty is not a function
63
63
```
64
64
65
-
As you can see, now`.filter`returns `Array`. So the extended functionality is not passed any further.
65
+
Come potete osservare, ora`.filter`restituisce un `Array`. Quindi l'estensione delle funzionalità non sarà più disponibile.
66
66
67
-
```smart header="Other collections work similarly"
68
-
Other collections, such as `Map` and `Set`, work alike. They also use `Symbol.species`.
67
+
```smart header="Le altre collezioni funzionano in maniera simile"
68
+
Le altre collezioni, come `Map` e `Set`, funzionano in maniera molto simile. Anche queste utilizzano `Symbol.species`.
69
69
```
70
70
71
-
## No static inheritance in built-ins
71
+
## Con gli oggetti built-in non si ereditano le proprietà statiche
72
72
73
-
Built-in objects have their own static methods, for instance`Object.keys`, `Array.isArray` etc.
73
+
Gli oggetti built-in possiedono i loro metodi statici, ad esempio`Object.keys`, `Array.isArray` etc.
74
74
75
-
As we already know, native classes extend each other. For instance, `Array`extends`Object`.
75
+
Come già sappiamo, le classi integrate si estendono a vicenda. Ad esempio, `Array`estende`Object`.
76
76
77
-
Normally, when one class extends another, both static and non-static methods are inherited. That was thoroughly explained in the article[](info:static-properties-methods#statics-and-inheritance).
77
+
Normalmente, quando una classe ne estende un'altra, sia i metodi statici che quelli non-statici vengono ereditati. Questo è stato ampiamente spiegato nell'articolo[](info:static-properties-methods#statics-and-inheritance).
78
78
79
-
But built-in classes are an exception. They don't inherit statics from each other.
79
+
Ma le classi built-in fanno eccezione. Queste, infatti, non ereditano i membri statici a le une dalle altre.
80
80
81
-
For example, both`Array`and`Date`inherit from`Object`, so their instances have methods from `Object.prototype`. But`Array.[[Prototype]]`does not reference `Object`, so there's no, for instance, `Array.keys()` (or`Date.keys()`) static method.
81
+
Ad esempio, sia`Array`che`Date`ereditano da`Object`, quindi le loro istanze possiedono i metodi di `Object.prototype`. Ma`Array.[[Prototype]]`non fa riferimento ad `Object`, quindi, ad esempio, non si ha alcun metodo statico come `Array.keys()` (o`Date.keys()`).
82
82
83
-
Here's the picture structure for `Date`and`Object`:
83
+
Qui vediamo raffigurata la struttura per `Date`e`Object`:
84
84
85
85

86
86
87
-
That's an important difference of inheritance between built-in objects compared to what we get with`extends`.
87
+
Questa è un'importante differenza dell'ereditarietà tra gli oggetti integrati, rispetto a quella che otteniamo tramite`extends`.
0 commit comments