Created
February 23, 2016 15:01
-
-
Save kikill95/159e115839b9009487b5 to your computer and use it in GitHub Desktop.
forEach for NodeList
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // forEach method, could be shipped as part of an Object Literal/Module | |
| var forEach = function (array, callback, scope) { | |
| for (var i = 0; i < array.length; i++) { | |
| callback.call(scope, i, array[i]); // passes back stuff we need | |
| } | |
| }; | |
| // Usage: | |
| // optionally change the scope as final parameter too, like ECMA5 | |
| var myNodeList = document.querySelectorAll('li'); | |
| forEach(myNodeList, function (index, value) { | |
| console.log(index, value); // passes index + value back! | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment