June 14
Node list is not array
Node list is a collection of DOM Nodes. Most of them who code in Firebug tend to consider the node list as array because it looks like any other array. Node list has just two properties length and index. If you want to iterate on that you can use the old school way of incrementing the index or do any of the Enumerable functions by passing node list to call method. Let me show how to do that.
Example:[div.post, div.post, div.post, div.post, div.post]
>>> Array.prototype.splice.call(foo)
>>> foo.each(function(item){console.log(item)})
<div class="post">
<div class="post">
<div class="post">
<div class="post">
<div class="post">
[div.post, div.post, div.post, div.post, div.post]
So what does call do? Call allows you to invoke a function as if it were a method of some other object. On above example foo is a node list its not a array but still I could able to splice that. ECMAScript specifies two methods call and apply. Both does relatively same job except the type of the argument. Apply takes a array as a argument.
Comments