--- title: NodeList.prototype.forEach() slug: Web/API/NodeList/forEach tags: - DOM - Iterable - Métodos - NodeList - Referencia - Web translation_of: Web/API/NodeList/forEach ---
{{APIRef("DOM")}}
El métodoforEach() de la interfase{{domxref("NodeList")}} llama a la función callback proporcionada como parámetro una vez para cadapar de valores en la lista, en el orden en que se insertaron.
nodeList.forEach(callback[, thisArg]);
callbackcurrentValuecurrentIndexlistObjforEach().thisArg {{Optional_inline}}callback.{{jsxref('undefined')}}.
Ninguna.
var nodo = document.createElement("div");
var infante1 = document.createElement("p");
var infante2 = document.createTextNode("hey");
var infante3 = document.createElement("span");
nodo.appendChild(infante1);
nodo.appendChild(infante2);
nodo.appendChild(infante3);
var list = nodo.childNodes;
list.forEach(
function(currentValue, currentIndex, listObj) {
console.log(currentValue + ', ' + currentIndex + ', ' + this);
},
'miEsteArg'
);
resulta en:
[object HTMLParagraphElement], 0, miEsteArg [object Text], 1, miEsteArg [object HTMLSpanElement], 2, miEsteArg
Este {{Glossary("Polyfill","polyfill")}} le da compatibilidad a todos los navegadores que soportan ES5:
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);
}
};
}
ó
if (window.NodeList && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = Array.prototype.forEach;
}
El comportamiento ateriror esta implementado en muchos navegadores. NodeList.prototype.forEach (Chrome, Firefox for example).
Especificación
| Status | Comentarios | |
|---|---|---|
| {{SpecName('DOM WHATWG', '#interface-nodelist', 'NodeList')}} | {{ Spec2('DOM WHATWG') }} | DefineNodeList como<Nodo>iterable |
| {{SpecName("WebIDL", "#es-forEach", "forEach")}} | {{Spec2("WebIDL")}} | DefineforEachen declaracionesiterables |
{{Compat("api.NodeList.forEach")}}