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
The assignment to`Rabbit.prototype`sets up`[[Prototype]]`for new objects, but it does not affect the existing ones.
6
+
A atribuição do`Rabbit.prototype`seta o`[[Prototype]]`para novos objetos, mas não afeta os objetos que já existem.
7
7
8
8
2.`false`.
9
9
10
-
Objects are assigned by reference. The object from`Rabbit.prototype`is not duplicated, it's still a single object referenced both by`Rabbit.prototype`and by the `[[Prototype]]`of`rabbit`.
10
+
Objetos são atrbuídos por referência. O objeto do`Rabbit.prototype`não é duplicado, ele continua sendo um único objeto refereciado por`Rabbit.prototype`e pelo `[[Prototype]]`do`rabbit`.
11
11
12
-
So when we change its content through one reference, it is visible through the other one.
12
+
Portanto, quando nós mudamos o seu conteúdo através de uma referência, ele fica visível para as outras.
13
13
14
14
3.`true`.
15
15
16
-
All `delete`operations are applied directly to the object. Here `delete rabbit.eats`tries to remove `eats`property from `rabbit`, but it doesn't have it. So the operation won't have any effect.
16
+
Todas as operações de `delete`são aplicadas diretamente ao objeto. Neste caso, `delete rabbit.eats`tenta remover a propriedade `eats`do `rabbit`, mas ele não a tem. Assim, essa operação não tem nenhum efeito.
17
17
18
18
4.`undefined`.
19
19
20
-
The property`eats`is deleted from the prototype, it doesn't exist any more.
20
+
A propriedade`eats`é deletada do protótipo, então ela realmente não existe mais.
Remember, new objects can be created with a constructor function, like`new F()`.
3
+
Lembre-se, novos objetos podem ser criados com uma função construtora, usando`new F()`.
4
4
5
-
If`F.prototype`is an object, then the `new`operator uses it to set `[[Prototype]]`for the new object.
5
+
Se`F.prototype`é um objeto, então o operador `new`usa ela para setar `[[Prototype]]`no novo objeto.
6
6
7
7
```smart
8
-
JavaScript had prototypal inheritance from the beginning. It was one of the core features of the language.
8
+
JavaScript tem herança prototipada desde o começo. Isso era uma das funcionalidades centrais da linguagem.
9
9
10
-
But in the old times, there was no direct access to it. The only thing that worked reliably was a `"prototype"` property of the constructor function, described in this chapter. So there are many scripts that still use it.
10
+
Mas antigamente não havia um acesso direto a ela. A única coisa que funcionava de forma confiável era uma propriedade `"prototype"` da função construtora, descrita nesse capítulo. Então, existem muitos scripts que ainda a utilizam.
11
11
```
12
12
13
-
Please note that`F.prototype`here means a regular property named`"prototype"`on `F`. It sounds something similar to the term "prototype", but here we really mean a regular property with this name.
13
+
Note que o`F.prototype`aqui significa uma propriedade regular de nome`"prototype"`dentro de `F`. Isso soa um pouco similar ao termo "prototype" (protótipo), mas aqui nós estamos falando realmente de uma propriedade regular com esse nome.
14
14
15
-
Here's the example:
15
+
Aqui temos um exemplo:
16
16
17
17
```js run
18
18
let animal = {
@@ -32,27 +32,27 @@ let rabbit = new Rabbit("White Rabbit"); // rabbit.__proto__ == animal
32
32
alert( rabbit.eats ); // true
33
33
```
34
34
35
-
Setting`Rabbit.prototype = animal`literally states the following: "When a`new Rabbit`is created, assign its`[[Prototype]]`to`animal`".
35
+
Setando`Rabbit.prototype = animal`literalmente significa o seguinte: "Quando o`new Rabbit`for criado, atribua seu`[[Prototype]]`para`animal`".
36
36
37
-
That's the resulting picture:
37
+
Essa é a imagem do resultado:
38
38
39
39

40
40
41
-
On the picture, `"prototype"`is a horizontal arrow, meaning a regular property, and`[[Prototype]]`is vertical, meaning the inheritance of`rabbit`from`animal`.
41
+
Na imagem, `"prototype"`é a seta na horizontal, indicando uma propriedade regular, e`[[Prototype]]`está na vertical, indicando a herança de`rabbit`vinda do`animal`.
42
42
43
-
```smart header="`F.prototype`only used at `new F` time"
44
-
`F.prototype`property is only used when `new F`is called, it assigns `[[Prototype]]`of the new object.
43
+
```smart header="`F.prototype`é usado apenas na chamada `new F`"
44
+
A propriedade `F.prototype`é usada apenas quando `new F`é chamado, e ela atribui um valor para o `[[Prototype]]`do novo objeto.
45
45
46
-
If, after the creation, `F.prototype`property changes (`F.prototype = <another object>`), then new objects created by`new F`will have another object as`[[Prototype]]`, but already existing objects keep the old one.
46
+
Se, depois da criação, a propriedade `F.prototype`mudar (`F.prototype = <another object>`), então novos objetos criados com`new F`vão ter outro objeto como`[[Prototype]]`, enquanto os objetos que já existirem vão manter o antigo.
47
47
```
48
48
49
-
## Default F.prototype, constructor property
49
+
## F.prototype default, propriedade do construtor
50
50
51
-
Every function has the `"prototype"` property even if we don't supply it.
51
+
Toda função tem a propriedade `"prototype"`, mesmo que não a use.
52
52
53
-
The default `"prototype"` is an object with the only property `constructor` that points back to the function itself.
53
+
O `"prototype"` default é um objeto com apenas uma propriedade `constructor` que aponta para a própria função a que pertence.
alert(rabbit.constructor== Rabbit); // true (vindo do protótipo)
87
87
```
88
88
89
89

90
90
91
-
We can use `constructor`property to create a new object using the same constructor as the existing one.
91
+
Nós podemos usar a propriedade `constructor`para criar um novo objeto usando o próprio construtor de um objeto que já exista.
92
92
93
-
Like here:
93
+
Como abaixo:
94
94
95
95
```js run
96
96
functionRabbit(name) {
@@ -105,17 +105,17 @@ let rabbit2 = new rabbit.constructor("Black Rabbit");
105
105
*/!*
106
106
```
107
107
108
-
That's handy when we have an object, don't know which constructor was used for it (e.g. it comes from a 3rd party library), and we need to create another one of the same kind.
108
+
Isso é prático quando nós temos um objeto, não sabemos qual construtor foi usado para ele (de uma biblioteca de terceiros, por exemplo), e nós precisamos criar outro objeto do mesmo tipo.
109
109
110
-
But probably the most important thing about `"constructor"`is that...
110
+
Mas provavelmente a coisa mais importante sobre o `"constructor"`é que...
111
111
112
-
**...JavaScript itself does not ensure the right `"constructor"` value.**
112
+
**...O próprio JavaScript não garante qual é o valor correto do `"constructor"`.**
113
113
114
-
Yes, it exists in the default `"prototype"`for functions, but that's all. What happens with it later -- is totally on us.
114
+
Sim, existe um `"prototype"`default para funções, mas é só isso. O que acontece com ele depois -- está totalmente por nossa conta.
115
115
116
-
In particular, if we replace the default prototype as a whole, then there will be no `"constructor"`in it.
116
+
Em particular, se nós substituirmos o `prototype` default como um todo, não vai haver um `"constructor"`nele.
So, to keep the right `"constructor"`we can choose to add/remove properties to the default `"prototype"`instead of overwriting it as a whole:
132
+
Portanto, para manter o `"constructor"`certo, nós podemos escolher adicionar/remover propriedades do `"prototype"`ao invés de sobrescrevê-lo completamente:
133
133
134
134
```js
135
135
functionRabbit() {}
136
136
137
-
//Not overwrite Rabbit.prototype totally
138
-
//just add to it
137
+
//não sobrescreva Rabbit.prototype completamente
138
+
//apenas adicione
139
139
Rabbit.prototype.jumps=true
140
-
//the default Rabbit.prototype.constructor is preserved
140
+
//o Rabbit.prototype.constructor default fica preservado
141
141
```
142
142
143
-
Or, alternatively, recreate the `constructor`property manually:
143
+
Outra alternativa é recriar a propriedade `constructor`manualmente:
144
144
145
145
```js
146
146
Rabbit.prototype= {
@@ -150,26 +150,27 @@ Rabbit.prototype = {
150
150
*/!*
151
151
};
152
152
153
-
//now constructor is also correct, because we added it
153
+
//agora o constructor também está correto, porque nós o adicionamos
154
154
```
155
155
156
156
157
-
## Summary
157
+
## Resumo
158
158
159
-
In this chapter we briefly described the way of setting a `[[Prototype]]` for objects created via a constructor function. Later we'll see more advanced programming patterns that rely on it.
159
+
Neste capítulo, nós descrevemos brevemente a forma de setar um `[[Prototype]]` para os objetos via função construtura. Mais tarde nós vamos ver padrões (*patterns*) mais avançados de programação que dependem disso.
160
160
161
-
Everything is quite simple, just a few notes to make things clear:
161
+
É tudo bem simples, mas aqui estão algumas notas para deixar as coisas claras:
162
162
163
-
- The `F.prototype` property (don't mistake it for `[[Prototype]]`) sets `[[Prototype]]` of new objects when `new F()` is called.
164
-
- The value of `F.prototype` should be either an object or `null`: other values won'twork.
165
-
-The`"prototype"`propertyonlyhassuchaspecialeffectwhensetonaconstructor function, and invoked with `new`.
163
+
- A propriedade `F.prototype` (não confunda com o `[[Prototype]]`) seta o `[[Prototype]]` de novos objetos quando `new F()` é chamado.
164
+
- O valor de `F.prototype` deveria ser ou um objeto ou `null`: outros valores não vão funcionar.
165
+
- A propriedade `"prototype"` só tem o efeito especial quando setada em uma função construtora, e invocada com `new`.
166
+
167
+
Em objetos regulares, o `prototype` não tem nada de especial:
166
168
167
-
On regular objects the `prototype` is nothing special:
168
169
```js
169
170
let user = {
170
171
name:"John",
171
-
prototype: "Bla-bla" //no magic at all
172
+
prototype:"Bla-bla"//nenhuma mágica aqui
172
173
};
173
174
```
174
175
175
-
By default all functions have `F.prototype = { constructor: F }`, so we can get the constructor of an object by accessing its `"constructor"` property.
176
+
Por definição, todas as funções possuem `F.prototype= { constructor: F }`, então nós podemos obter o construtor de um objeto acessando sua propriedade `"constructor"`.
0 commit comments