From 1109132f09d75da9a28b649c7677bb6ce07c40c0 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:41:45 -0500 Subject: initial commit --- files/es/web/api/nodelist/foreach/index.html | 132 +++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 files/es/web/api/nodelist/foreach/index.html (limited to 'files/es/web/api/nodelist/foreach') diff --git a/files/es/web/api/nodelist/foreach/index.html b/files/es/web/api/nodelist/foreach/index.html new file mode 100644 index 0000000000..4cb55533e1 --- /dev/null +++ b/files/es/web/api/nodelist/foreach/index.html @@ -0,0 +1,132 @@ +--- +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.

+ +

Syntax

+ +
nodeList.forEach(callback[, thisArg]);
+
+ +

Parámetros

+ +
+
callback
+
Función a ser ejecutada paracada elemento, tomando eventualmente 3 argumentos: +
+
currentValue
+
El valor que esta siendo procesado en la lista de nodos.
+
currentIndex
+
El índice del elemento que esta siendo procesado en la lista de nodos.
+
listObj
+
El objeto NodeList al que se está aplicando el métodoforEach().
+
+
+
thisArg {{Optional_inline}}
+
Valor a ser usado como {{jsxref("this")}} al ejecutarcallback.
+
+ +

Valor Retornado

+ +

{{jsxref('undefined')}}.

+ +

Excepciones

+ +

Ninguna.

+ +

Ejemplo

+ +
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
+ +

Polyfill

+ +

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).

+ +

Especificaciones

+ +

Especificación

+ + + + + + + + + + + + + + + + + + + + + +
StatusComentarios
{{SpecName('DOM WHATWG', '#interface-nodelist', 'NodeList')}}{{ Spec2('DOM WHATWG') }}DefineNodeList como<Nodo>iterable
{{SpecName("WebIDL", "#es-forEach", "forEach")}}{{Spec2("WebIDL")}}DefineforEachen declaracionesiterables
+ +

Compatibilidad en Navegadores

+ + + +

{{Compat("api.NodeList.forEach")}}

+ +

Ver también

+ + -- cgit v1.2.3-54-g00ecf