Skip to content

Commit cb2fa2e

Browse files
authored
Merge pull request #213 from longo-andrea/article/prototype-methods
Prototype methods, objects without __proto__
2 parents 52b7e58 + ec3bdcc commit cb2fa2e

File tree

5 files changed

+92
-92
lines changed

5 files changed

+92
-92
lines changed
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11

2-
The method can take all enumerable keys using `Object.keys` and output their list.
2+
Il metodo `Object.keys` mostra tutte le proprietà enumerabili di un oggetto.
33

4-
To make `toString` non-enumerable, let's define it using a property descriptor. The syntax of `Object.create` allows us to provide an object with property descriptors as the second argument.
4+
Per rendere `toString` non-enumerable, dobbiamo definirlo utilizzando un property descriptor. La sintassi che ci permette di farlo è `Object.create`, che ci consente di fornire dei property descriptors come secondo argomento.
55

66
```js run
77
*!*
88
let dictionary = Object.create(null, {
9-
toString: { // define toString property
10-
value() { // the value is a function
9+
toString: { // definiamo la proprietà toString
10+
value() { // il valore è una funzione
1111
return Object.keys(this).join();
1212
}
1313
}
@@ -17,15 +17,15 @@ let dictionary = Object.create(null, {
1717
dictionary.apple = "Apple";
1818
dictionary.__proto__ = "test";
1919

20-
// apple and __proto__ is in the loop
20+
// apple e __proto__ appaiono nel ciclo
2121
for(let key in dictionary) {
22-
alert(key); // "apple", then "__proto__"
22+
alert(key); // "apple", poi "__proto__"
2323
}
2424

25-
// comma-separated list of properties by toString
25+
// vengono elencate le proprietà separate da una virgola
2626
alert(dictionary); // "apple,__proto__"
2727
```
2828

29-
When we create a property using a descriptor, its flags are `false` by default. So in the code above, `dictionary.toString` is non-enumerable.
29+
Possiamo crare una proprietà utilizzando un descriptor. Di default i flag vengono impostati a `false`. Quindi nel codice sopra, `dictionary.toString` è non-enumerable.
3030

31-
See the the chapter [](info:property-descriptors) for review.
31+
Vedi il capitolo [property descriptors](info:property-descriptors) se hai bisogno di ripassare l'argomento.

1-js/08-prototypes/04-prototype-methods/2-dictionary-tostring/task.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@ importance: 5
22

33
---
44

5-
# Add toString to the dictionary
5+
# Aggiungi toString al dizionario
66

7-
There's an object `dictionary`, created as `Object.create(null)`, to store any `key/value` pairs.
7+
Abbiamo un oggetto `dictionary`, creato come `Object.create(null)`, in cui memorizziamo coppie `key/value`.
88

9-
Add method `dictionary.toString()` into it, that should return a comma-delimited list of keys. Your `toString` should not show up in `for..in` over the object.
9+
Aggiungi un metodo `dictionary.toString()`, il quale dovrebbe ritornare un lista di chiavi separate da virgola. Il metodo `toString` non deve essere mostrato nei cicli `for..in`.
1010

11-
Here's how it should work:
11+
Dovrebbe funzionare così:
1212

1313
```js
1414
let dictionary = Object.create(null);
1515

1616
*!*
17-
// your code to add dictionary.toString method
17+
// il tuo codice per aggiungere il metodo toString
1818
*/!*
1919

20-
// add some data
20+
// aggiungiamo dei valori
2121
dictionary.apple = "Apple";
22-
dictionary.__proto__ = "test"; // __proto__ is a regular property key here
22+
dictionary.__proto__ = "test"; // __proto__ è una proprietà comune in questo caso
2323

24-
// only apple and __proto__ are in the loop
24+
// nel ciclo compaiono solo apple e __proto__
2525
for(let key in dictionary) {
26-
alert(key); // "apple", then "__proto__"
26+
alert(key); // "apple", poi "__proto__"
2727
}
2828

29-
// your toString in action
29+
// la tua implementazione di toString in azione
3030
alert(dictionary); // "apple,__proto__"
3131
```

1-js/08-prototypes/04-prototype-methods/3-compare-calls/solution.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
The first call has `this == rabbit`, the other ones have `this` equal to `Rabbit.prototype`, because it's actually the object before the dot.
2+
La prima chiamata ha `this == rabbit`, le altre hanno `this` uguale a `Rabbit.prototype`, perché è l'oggetto prima del punto.
33

4-
So only the first call shows `Rabbit`, other ones show `undefined`:
4+
Quindi, solamente la prima chiamata mostra `Rabbit`, le altre mostrano `undefined`:
55

66
```js run
77
function Rabbit(name) {

1-js/08-prototypes/04-prototype-methods/3-compare-calls/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 5
22

33
---
44

5-
# The difference between calls
5+
# La differenza tra chiamate
66

7-
Let's create a new `rabbit` object:
7+
Creiamo un nuovo oggetto `rabbit`:
88

99
```js
1010
function Rabbit(name) {
@@ -17,7 +17,7 @@ Rabbit.prototype.sayHi = function() {
1717
let rabbit = new Rabbit("Rabbit");
1818
```
1919

20-
These calls do the same thing or not?
20+
Queste chiamata fanno la stessa cosa o no?
2121

2222
```js
2323
rabbit.sayHi();

0 commit comments

Comments
 (0)