Javascript data structures - a collection object
Thursday, 28 October 2010
Article Index
Javascript data structures - a collection object
Managing the collection
Enumerators

Banner

Enumeration

Now we come to the tricky problem of enumerating a collection. You clearly cannot just use for in on the collection object e.g.

for(index in myCol)
{
alert(myCol[index]);
}

This works but it displays the properties and methods of myCol not the collection it contains.

One solution is to enumerate the collection property:

for (index in myCol.collection)
{
alert(myCol.item(index));
}

This has the advantage the the for in is used but the disadvantage that the user has to remember to use the collection property. It also doesn't work if you want the collection property to be inaccessible.

Perhaps the time has come to give up enumeration using for in and provide an enumeration method. There are ways to create very general enumeration methods that can be run on any object but there are advantages to creating custom enumeration methods for each type of data object you create. So let's add a ForEach method to the collection object:

this.forEach=function(block)
{
for (key in this.collection)
{
if(this.collection.hasOwnProperty(key))
{
block(this.collection[key]);
}
}
}

The basic idea is that the enumerator is passed a block of code i.e. a function that it will execute once for each item in the collection. The item in question is passed to the block of code as the first parameter.

The way that this works is that a standard for in loop is used to step though the properties of the collection. Just in case someone has added properties and methods to the prototype of the collection we check that each key corresponds to an own property i.e. not provided by a prototype object. In most cases this should be unnecessary but it is included to show how the approach could be made defensive.

Finally the block of code is called with the item as the first parameter. Notice that the code block doesn't run in the context of the collection object i.e. it can't make use of this and it can't be a method belonging to another object. This might seem restrictive but the idea is that the block of code is passed to the enumerator as if it was a lambda expression or a Ruby block. For example:

myCol.forEach(
function(item)
{
alert(item);
});

The function is called once with item set to each item in the collection. Notice that you don't have to name the first parameter item and the function can have as many lines as it needs.

You can modify the forEach to pass the item and key if that would be more useful and you can drop the restriction on using methods by changing the call to the block of code to block.call(object,item) where object is that object that the method belongs to passed into the forEach.

Once you have seen how a collection object can be implemented it is fairly easy to customise and extend to other collection subtypes.

 

See also:

 

 

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook , on Digg or you can subscribe to our weekly newsletter.

 

 

Banner


Javascript data structures - the binary tree

Binary trees in JavaScript? Easy with the right storage mapping function. Find out how to code a binary tree right up to a depth first traversal.



Just JavaScript - Object Construction

Object creation is fundamental to all object-oriented languages, but in JavaScript is is left to the programmer to work out how best to do it and often the practice that you encounter isn't the best b [ ... ]


Other Articles

<ASIN:1470525932>

<ASIN:0596517742>

<ASIN:1590597273>



Last Updated ( Tuesday, 15 January 2013 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.