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
A função `elem.hasChildNodes()` verifica se há algum nó filho.
129
129
130
-
### DOM collections
130
+
### Coleções DOM
131
131
132
-
As we can see, `childNodes` looks like an array. But actually it's not an array, but rather a *collection* -- a special array-like iterable object.
132
+
Como podemos ver, `childNodes` se parece com uma array. Mas, na verdade, não é uma array, mas sim uma *coleção* -- um objeto iterável semelhante a uma array.
133
133
134
-
There are two important consequences:
134
+
Há duas implicações importantes:
135
135
136
-
1. We can use `for..of` to iterate over it:
136
+
1. Podemos usar `for..of` para iterar sobre:
137
137
```js
138
138
for (let node of document.body.childNodes) {
139
-
alert(node); // shows all nodes from the collection
139
+
alert(node); // mostra todos os nós da coleção
140
140
}
141
141
```
142
-
That's because it's iterable (provides the `Symbol.iterator` property, as required).
142
+
Isso ocorre porque é iterável (fornece a propriedade `Symbol.iterator`, quando necessário).
143
143
144
-
2. Array methods won't work, because it's not an array:
144
+
2. Métodos de array não funcionam, porque não é uma array:
145
145
```js run
146
-
alert(document.body.childNodes.filter); // undefined (there's no filter method!)
146
+
alert(document.body.childNodes.filter); // undefined (não possui o método filter!)
147
147
```
148
148
149
-
The first thing is nice. The second is tolerable, because we can use `Array.from` to create a "real" array from the collection, if we want array methods:
149
+
A primeira implicação é boa. A segunda é tolerável, porque podemos usar `Array.from` para criar uma array "real" a partir da coleção, se quisermos métodos de array:
150
150
151
151
```js run
152
-
alert( Array.from(document.body.childNodes).filter ); // now it's there
152
+
alert( Array.from(document.body.childNodes).filter ); // function
153
153
```
154
154
155
-
```warn header="DOM collections are read-only"
156
-
DOM collections, and even more -- *all* navigation properties listed in this chapter are read-only.
155
+
```warn header="Coleções DOM são somente leitura"
156
+
Coleções DOM e muito mais -- *todas* as propriedades de navegação listadas neste capítulo são somente leitura.
157
157
158
-
We can't replace a child by something else by assigning `childNodes[i] = ...`.
158
+
Não podemos substituir um filho através de uma atribuição: `childNodes[i] = ...`.
159
159
160
-
Changing DOM needs other methods. We will see them in the next chapter.
160
+
São necessários outros métodos para alterar o DOM. Veremos no próximo capítulo.
161
161
```
162
162
163
-
```warn header="DOM collections are live"
164
-
Almost all DOM collections with minor exceptions are *live*. In other words, they reflect the current state of DOM.
163
+
```warn header="Coleções DOM estão ativas"
164
+
Quase todas as coleções DOM, com pequenas exceções, são *ativas*. Em outras palavras, elas refletem o estado atual do DOM.
165
165
166
-
If we keep a reference to `elem.childNodes`, and add/remove nodes into DOM, then they appear in the collection automatically.
166
+
Se mantivermos uma referência ao `elem.childNodes` e adicionarmos nós no DOM, eles aparecerão automaticamente na coleção.
167
167
```
168
168
169
-
````warn header="Don't use `for..in` to loop over collections"
170
-
Collections are iterable using `for..of`. Sometimes people try to use `for..in` for that.
169
+
````warn header="Não use `for..in` para iterar coleções"
170
+
As coleções são iteráveis usando `for..of`. Às vezes as pessoas tentam usar `for..in` para isso.
171
171
172
-
Please, don't. The `for..in` loop iterates over all enumerable properties. And collections have some "extra" rarely used properties that we usually do not want to get:
172
+
Por favor, não! O loop `for..in` itera sobre todas as propriedades enumeráveis. E as coleções têm algumas propriedades "extras" raramente usadas que, geralmente, não queremos obter:
173
173
174
174
```html run
175
175
<body>
176
176
<script>
177
-
// shows 0, 1, length, item, values and more.
177
+
// mostra: 0, 1, length, item, values...
178
178
for (let prop in document.body.childNodes) alert(prop);
0 commit comments