1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
---
title: NodeList.prototype.forEach()
slug: Web/API/NodeList/forEach
tags:
- DOM
- Iterabile
- NodeList
- Referenza
- Web
- metodo
translation_of: Web/API/NodeList/forEach
---
<p>{{APIRef("DOM")}}</p>
<p>Il metodo <strong><code>forEach()</code></strong> dell'interfaccia {{domxref("NodeList")}} chiama il callback fornito nel parametro una volta per ogni coppia di valori nell'elenco, in ordine di inserimento.</p>
<h2 id="Sintassi">Sintassi</h2>
<pre class="syntaxbox"><em>NodeList.</em>forEach<em>(callback[, thisArg]);</em>
</pre>
<h3 id="Parametri">Parametri</h3>
<dl>
<dt><code>callback</code></dt>
<dd>Funzione da eseguire per ciascun elemento, eventualmente con 3 argomenti:
<dl>
<dt><em><code>currentValue</code></em></dt>
<dd>L'elemento corrente in elaborazione nella NodeList.</dd>
<dt><code><em>currentIndex</em></code></dt>
<dd>L'indice dell'elemento corrente in fase di elaborazione nella NodeList.</dd>
<dt><em><code>listObj</code></em></dt>
<dd>La NodeList a cui viene applicato <code>forEach()</code>.</dd>
</dl>
</dd>
<dt><code>thisArg</code><code> {{Optional_inline}}</code></dt>
<dd>Valore da utilizzare come {{jsxref("this")}} quando viene eseguito <code>callback</code>.</dd>
</dl>
<h3 id="Valore_di_ritorno">Valore di ritorno</h3>
<p>{{jsxref('undefined')}}.</p>
<h2 id="Eccezioni">Eccezioni</h2>
<p><em>Nessuna</em>.</p>
<h2 id="Esempio">Esempio</h2>
<pre class="brush: js;highlight:[6]">var node = document.createElement("div");
var kid1 = document.createElement("p");
var kid2 = document.createTextNode("hey");
var kid3 = document.createElement("span");
node.appendChild(kid1);
node.appendChild(kid2);
node.appendChild(kid3);
var list = node.childNodes;
list.forEach(
function(currentValue, currentIndex, listObj) {
console.log(currentValue + ', ' + currentIndex + ', ' + this);
},
'myThisArg'
);</pre>
<p>ritorna:</p>
<pre>[object HTMLParagraphElement], 0, myThisArg
[object Text], 1, myThisArg
[object HTMLSpanElement], 2, myThisArg</pre>
<h2 id="Polyfill">Polyfill</h2>
<p>Questo {{Glossary("Polyfill","polyfill")}} aggiunge compatibilità a tutti i browser che supportano <a href="https://caniuse.com/#search=es5">ES5</a>:</p>
<pre class="brush: js">if (window.NodeList && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = function (callback, thisArg) {
thisArg = thisArg || window;
for (var i = 0; i < this.length; i++) {
callback.call(thisArg, this[i], i, this);
}
};
}</pre>
<p>OPPURE</p>
<pre class="brush: js">if (window.NodeList && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = Array.prototype.forEach;
}</pre>
<p>Il comportamento sopra riportato indica il numero di browser che implementa effettivamente NodeList.prototype.forEach (Chrome, ad esempio).</p>
<h2 id="Specifiche">Specifiche</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specifica</th>
<th scope="col">Stato</th>
<th scope="col">Commento</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("WebIDL", "#es-forEach", "forEach")}}</td>
<td>{{Spec2("WebIDL")}}</td>
<td>Definisce <code>forEach</code> sulle dichiarazioni <code>iterable</code></td>
</tr>
</tbody>
</table>
<h2 id="Compatibilità_con_i_browser">Compatibilità con i browser</h2>
<p>{{Compat("api.NodeList.forEach")}}</p>
<h2 id="Vedi_anche">Vedi anche</h2>
<ul>
<li>{{domxref("Node")}}</li>
<li>{{domxref("NodeList")}}</li>
</ul>
|