Skip to content
Open
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.
O método pode pegar todas as chaves enumeráveis usando `Object.keys` e exibir a lista delas.

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.
Para tornar o `toString` não enumerável, vamos defini-lo usando um descritor de propriedade. A sintaxe do `Object.create` nos permite fornecer um objeto com descritores de propriedade como segundo argumento.

```js run
*!*
let dictionary = Object.create(null, {
toString: { // define toString property
value() { // the value is a function
toString: { // define a propriedade toString
value() { // o valor é uma função
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__ estão no laço
for(let key in dictionary) {
alert(key); // "apple", then "__proto__"
alert(key); // "apple", depois "__proto__"
}

// comma-separated list of properties by toString
// lista de propriedades separadas por vírgula, via toString
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.
Quando criamos uma propriedade usando um descritor, seus sinalizadores são `false` por padrão. Então, no código acima, `dictionary.toString` é não enumerável.

See the chapter [](info:property-descriptors) for review.
Veja o capítulo [](info:property-descriptors) pra revisar.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ importance: 5

---

# Add toString to the dictionary
# Adicione toString ao dicionário

There's an object `dictionary`, created as `Object.create(null)`, to store any `key/value` pairs.
Existe um objeto `dictionary`, criado como `Object.create(null)`, para armazenar quaisquer pares `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.
Adicione o método `dictionary.toString()` a ele, que deve retornar uma lista de chaves separadas por vírgula. Seu `toString` não deve aparecer no `for..in` sobre o objeto.

Here's how it should work:
Veja como deve funcionar:

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

*!*
// your code to add dictionary.toString method
// seu código para adicionar o método dictionary.toString
*/!*

// add some data
// adiciona alguns dados
dictionary.apple = "Apple";
dictionary.__proto__ = "test"; // __proto__ is a regular property key here
dictionary.__proto__ = "test"; // __proto__ é uma chave de propriedade normal aqui

// only apple and __proto__ are in the loop
// só apple e __proto__ estão no laço
for(let key in dictionary) {
alert(key); // "apple", then "__proto__"
alert(key); // "apple", depois "__proto__"
}

// your toString in action
// seu toString em ação
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.
A primeira chamada tem `this == rabbit`, as outras têm `this` igual a `Rabbit.prototype`, porque ele é, na verdade, o objeto antes do ponto.

So only the first call shows `Rabbit`, other ones show `undefined`:
Então só a primeira chamada mostra `Rabbit`, as outras mostram `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
# A diferença entre as chamadas

Let's create a new `rabbit` object:
Vamos criar um novo objeto `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?
Essas chamadas fazem a mesma coisa ou não?

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