Instantly share code, notes, and snippets.

Embed
What would you like to do?
nodelists to arrays

This code transparently makes nodelists act like arrays!

With it, you can do this:

document.getElementsByTagName('a').forEach(function(a){

a.style.color = '#F00'

})​

(function(a,b,c,d){ //start of self-calling function
for(b in a=Object.getOwnPropertyNames(c=Array.prototype)) //for every array method
NodeList.prototype[a[b]]=c[a[b]] //add it to nodelists
})() //end of self-calling function
(function(a,b,c,d){for(b in a=Object.getOwnPropertyNames(c=Array.prototype))NodeList.prototype[a[b]]=c[a[b]]})()
{
"name": "listToArray",
"description": "Makes nodelists act like arrays",
"keywords": [
"nodelist",
"array",
"prototype",
"foreach"
]
}
<!DOCTYPE html>
<title>Foo</title>
​<a>lolol</a>
<a>efesgdsge</a>
<a>rdgsdrhrsd</a>
<a>rgdsgdsg</a>
<a>eswred</a>
<a>dfsdghtyj</a>
<a>srhdhthtj</a>
<script>
(function(a,b,c,d){for(b in a=Object.getOwnPropertyNames(c=Array.prototype))NodeList.prototype[a[b]]=c[a[b]]})()
document.getElementsByTagName('a').forEach(function(a){
a.style.color = '#F00'
})​
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment