Last active
September 15, 2015 13:58
-
-
Save fschell/5bbb704fe9b26137166b to your computer and use it in GitHub Desktop.
NodeList to Array
This file contains 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
// create array from NodeList without modifying NodeList Prototype | |
var arr = Array.prototype.slice.call( document.querySelectorAll('div') ); | |
// extend NodeList prototype... | |
NodeList.prototype.array = function() { | |
return Array.prototype.slice.call(this); | |
}; | |
// ...and use it as chained method | |
var arr = document.querySelectorAll('div').array(); | |
// now we can use forEach | |
arr.forEach(function(e,i){ | |
console.log(e) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment