Skip to content

Prototype methods, objects without __proto__ #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

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

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

```js run
*!*
let dictionary = Object.create(null, {
toString: { // define toString property
value() { // the value is a function
toString: { // definiamo la proprietà toString
value() { // il valore è una funzione
return Object.keys(this).join();
}
}
Expand All @@ -17,15 +17,15 @@ let dictionary = Object.create(null, {
dictionary.apple = "Apple";
dictionary.__proto__ = "test";

// apple and __proto__ is in the loop
// apple e __proto__ appaiono nel ciclo
for(let key in dictionary) {
alert(key); // "apple", then "__proto__"
alert(key); // "apple", poi "__proto__"
}

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

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

See the the chapter [](info:property-descriptors) for review.
Vedi il capitolo [property descriptors](info:property-descriptors) se hai bisogno di ripassare l'argomento.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ importance: 5

---

# Add toString to the dictionary
# Aggiungi toString al dizionario

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

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

Here's how it should work:
Dovrebbe funzionare così:

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

*!*
// your code to add dictionary.toString method
// il tuo codice per aggiungere il metodo toString
*/!*

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

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

// your toString in action
// la tua implementazione di toString in azione
alert(dictionary); // "apple,__proto__"
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

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

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

```js run
function Rabbit(name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# The difference between calls
# La differenza tra chiamate

Let's create a new `rabbit` object:
Creiamo un nuovo oggetto `rabbit`:

```js
function Rabbit(name) {
Expand All @@ -17,7 +17,7 @@ Rabbit.prototype.sayHi = function() {
let rabbit = new Rabbit("Rabbit");
```

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

```js
rabbit.sayHi();
Expand Down
Loading