Skip to content

Commit e3810c6

Browse files
authored
Merge pull request #237 from longo-andrea/article/extend-builtin
Extending built-in classes
2 parents 0bd7370 + 5fc16b7 commit e3810c6

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
# Extending built-in classes
2+
# Estendere le classi built-in
33

4-
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.
55

6-
For instance, here `PowerArray` inherits from the native `Array`:
6+
Ad esempio, qui vediamo `PowerArray` ereditare dall'`Array` nativo:
77

88
```js run
9-
// add one more method to it (can do more)
9+
// aggiungiamo un metodo (possiamo fare di più)
1010
class PowerArray extends Array {
1111
isEmpty() {
1212
return this.length === 0;
@@ -21,20 +21,20 @@ alert(filteredArr); // 10, 50
2121
alert(filteredArr.isEmpty()); // false
2222
```
2323

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.
2525

26-
In the example above,
26+
Nell'esempio sopra,
2727
```js
2828
arr.constructor === PowerArray
2929
```
3030

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.
3232

33-
Even more, we can customize that behavior.
33+
Inoltre, possiamo personalizzarne il comportamento.
3434

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.
3636

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:
3838

3939
```js run
4040
class PowerArray extends Array {
@@ -43,7 +43,7 @@ class PowerArray extends Array {
4343
}
4444

4545
*!*
46-
// built-in methods will use this as the constructor
46+
// i metodi built-in lo utilizzeranno come costruttore
4747
static get [Symbol.species]() {
4848
return Array;
4949
}
@@ -53,35 +53,35 @@ class PowerArray extends Array {
5353
let arr = new PowerArray(1, 2, 5, 10, 50);
5454
alert(arr.isEmpty()); // false
5555

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
5757
let filteredArr = arr.filter(item => item >= 10);
5858

5959
*!*
60-
// filteredArr is not PowerArray, but Array
60+
// filteredArr non è di tipo PowerArray, ma è un Array standard
6161
*/!*
6262
alert(filteredArr.isEmpty()); // Error: filteredArr.isEmpty is not a function
6363
```
6464

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.
6666

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`.
6969
```
7070

71-
## No static inheritance in built-ins
71+
## Con gli oggetti built-in non si ereditano le proprietà statiche
7272

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.
7474

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`.
7676

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).
7878

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.
8080

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()`).
8282

83-
Here's the picture structure for `Date` and `Object`:
83+
Qui vediamo raffigurata la struttura per `Date` e `Object`:
8484

8585
![](object-date-inheritance.svg)
8686

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

Comments
 (0)