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
Copy file name to clipboardExpand all lines: 1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/solution.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
2
-
1.Let's add`__proto__`:
2
+
1.Vamos adicionar`__proto__`:
3
3
4
4
```js run
5
5
let head = {
@@ -27,6 +27,6 @@
27
27
alert( table.money ); // undefined
28
28
```
29
29
30
-
2.In modern engines, performance-wise, there's no difference whether we take a property from an object or its prototype. They remember where the property was found and reuse it in the next request.
30
+
2.Em interpretadores de JavaScript modernos, em termos de performance (*performance-wise*), não há diferença entre obtermos uma propriedade de um objeto ou do seu protótipo. Eles se lembram onde a propriedade foi encontrada e reutilizam isso na próxima requisição.
31
31
32
-
For instance, for `pockets.glasses` they remember where they found `glasses` (in `head`), and next time will search right there. They are also smart enough to update internal caches if something changes, so that optimization is safe.
32
+
Por exemplo, para`pockets.glasses`eles se lembram onde encontraram`glasses` (em`head`), e na próxima vez vão procurar lá. Eles também são inteligentes o suficiente para atualizar caches internos se algo mudar, então essa otimização é segura.
Copy file name to clipboardExpand all lines: 1-js/08-prototypes/01-prototype-inheritance/2-search-algorithm/task.md
+5-5
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@ importance: 5
2
2
3
3
---
4
4
5
-
# Searching algorithm
5
+
# Algoritmo de busca
6
6
7
-
The task has two parts.
7
+
A tarefa tem duas partes.
8
8
9
-
Given the following objects:
9
+
Dados os objetos a seguir:
10
10
11
11
```js
12
12
let head = {
@@ -27,5 +27,5 @@ let pockets = {
27
27
};
28
28
```
29
29
30
-
1. Use `__proto__`to assign prototypes in a way that any property lookup will follow the path: `pockets` -> `bed` -> `table` -> `head`. For instance, `pockets.pen`should be `3` (found in`table`), and`bed.glasses`should be `1` (found in`head`).
31
-
2.Answer the question: is it faster to get `glasses`as`pockets.glasses`or `head.glasses`? Benchmark if needed.
30
+
1. Use `__proto__`para atribuir propriedades de forma que qualquer busca de propriedades siga o caminho: `pockets` -> `bed` -> `table` -> `head`. Por exemplo, `pockets.pen`deve ter o valor `3` (encontrado em`table`), e`bed.glasses`deve ter o valor `1` (encontrado em`head`).
31
+
2.Responda à seguinte questão: é mais rápido obter `glasses`como`pockets.glasses`ou como `head.glasses`? Compare (*benchmark*), se necessário.
Copy file name to clipboardExpand all lines: 1-js/08-prototypes/01-prototype-inheritance/4-hamster-proto/solution.md
+22-22
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,26 @@
1
-
Let's look carefully at what's going on in the call`speedy.eat("apple")`.
1
+
Vamos ver cuidadosamente o que está acontecendo na chamada`speedy.eat("apple")`.
2
2
3
-
1.The method`speedy.eat`is found in the prototype (`=hamster`), then executed with`this=speedy` (the object before the dot).
3
+
1.O método`speedy.eat`é encontrado no protótipo (`=hamster`), e executado usando`this=speedy` (o objeto antes do ponto).
4
4
5
-
2.Then `this.stomach.push()`needs to find `stomach`property and call`push`on it. It looks for `stomach`in`this` (`=speedy`), but nothing found.
5
+
2.Então o método `this.stomach.push()`precisa de encontrar uma propriedade `stomach`e chamar o`push`nela. Ele procura por um `stomach`no`this` (`=speedy`), mas não encontra.
6
6
7
-
3.Then it follows the prototype chain and finds `stomach`in`hamster`.
7
+
3.Aí ele segue a cadeia de protótipos e encontra `stomach`no`hamster`.
8
8
9
-
4.Then it calls `push` on it, adding the food into *the stomach of the prototype*.
9
+
4.Por fim, ele chama o `push`, adicionando a comida (*food*) dentro do *`stomach` do protótipo*.
10
10
11
-
So all hamsters share a single stomach!
11
+
Então, todos os hamsters compartilham o mesmo estômago!
12
12
13
-
Both for`lazy.stomach.push(...)`and`speedy.stomach.push()`, the property`stomach`is found in the prototype (as it's not in the object itself), then the new data is pushed into it.
13
+
Para ambos`lazy.stomach.push(...)`e`speedy.stomach.push()`, a propriedade`stomach`é encontrada no protótipo (porque não está no próprio objeto), e assim os novos dados são colocados nela.
14
14
15
-
Please note that such thing doesn't happen in case of a simple assignment`this.stomach=`:
15
+
Note que isso não acontece no caso de uma simples atribuição`this.stomach=`:
16
16
17
17
```js run
18
18
let hamster = {
19
19
stomach: [],
20
20
21
21
eat(food) {
22
22
*!*
23
-
//assign to this.stomach instead of this.stomach.push
23
+
//atribui o valor para this.stomach ao invés de usar this.stomach.push
24
24
this.stomach= [food];
25
25
*/!*
26
26
}
@@ -34,17 +34,17 @@ let lazy = {
34
34
__proto__: hamster
35
35
};
36
36
37
-
// Speedy one found the food
38
-
speedy.eat("apple");
39
-
alert( speedy.stomach ); //apple
37
+
//O Speedy acha a comida
38
+
speedy.eat("maçã");
39
+
alert( speedy.stomach ); //maçã
40
40
41
-
//Lazy one's stomach is empty
42
-
alert( lazy.stomach ); // <nothing>
41
+
//O estômago do Lazy continua vazio
42
+
alert( lazy.stomach ); // <vazio>
43
43
```
44
44
45
-
Now all works fine, because`this.stomach=`does not perform a lookup of `stomach`. The value is written directly into`this`object.
45
+
Agora tudo funciona bem, porque`this.stomach=`não procura por um `stomach`. O valor é escrito diretamente no`this`do objeto.
46
46
47
-
Also we can totally avoid the problem by making sure that each hamster has their own stomach:
47
+
Além disso, nós podemos evitar completamente o problema fazendo com que cada hamster tenha seu próprio estômago:
48
48
49
49
```js run
50
50
let hamster = {
@@ -69,12 +69,12 @@ let lazy = {
69
69
*/!*
70
70
};
71
71
72
-
// Speedy one found the food
73
-
speedy.eat("apple");
74
-
alert( speedy.stomach ); //apple
72
+
//O Speedy acha a comida
73
+
speedy.eat("maçã");
74
+
alert( speedy.stomach ); //maçã
75
75
76
-
//Lazy one's stomach is empty
77
-
alert( lazy.stomach ); // <nothing>
76
+
//O estômago do Lazy continua vazio
77
+
alert( lazy.stomach ); // <vazio>
78
78
```
79
79
80
-
As a common solution, all properties that describe the state of a particular object, like `stomach`above, should be written into that object. That prevents such problems.
80
+
É uma solução comum, fazer com que todas as propriedades que descrevem um estado particular do objeto, como o `stomach`acima, sejam escritas dentro do próprio objeto. Isso previne esses problemas.
0 commit comments