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

Javascript has some good basic facilities for implementing data structures without too much effort. In this article the data structure under review is the collection, including an enumerator.

This is the first of a series of articles on implementing data structures in JavaScript.

See also:

See also:

 

 

Banner

Associative array

The associative array is the fundamental Javascript data structure. Every object in Javascript is an associative array as well as being whatever else you designed it to be.

For example:

var myArray=new Object();

myArray["A"]=0;
myArray["B"]=1;
myArray["C"]=2;

This creates an associative array and you can retrieve values using array indexing:

alert(myArray["B"]);

or standard object qualified naming:

alert(myArray.B);

Which you use just depend on how you are thinking of the instance - as an object or as an associative array. You can also create an associative array using Object Literal notation.

For example

var  MyArray={A:0,B:1,C:2};

creates the same array.

So the only real problem with using an associative array is that it can look a lot like an object!

This really only causes a problem when you add methods non-data properties to an object that you are treating as an associative array. For example, the only way to step though the contents of an associative array is to use the For in loop:

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

This works perfectly but if myArray has had say a Count method added to return the number of items in the array then the for in loop includes the method.

That is if you try

var  MyArray={A:0,B:1,C:2};
MyArray.Count=function()
{
return 3
};

for(index in MyArray)
{
alert(index);
};

You will see displayed A, B C, Count - that is the associative array has four items not three.

The situation is even worse in that that if MyArray has any prototype properties added to it then these too are included in the for in loop.

This mixing of associative data and properties is a problem if you are trying to create data handling objects. For example, if you want to create a collection object you can't simply add methods to the object that is going to be used to store the collection and expect for in to work.

What this means is that you either have to abandon for in or you have to modify the way that it is used slightly. As we will see there really isn't a 100% good solution to the problem but the end results are very usable.

Collection object

Let's take the approach of using an internal object to store the actual data and add methods to manipulate it. The new Collection object has a count property and a collection property.

var Collection=function()
{
this.count=0;
this.collection={};

The collection property could be made private using any of the standard techniques if you want to restrict access to it.

Banner

<ASIN:0596805527>

<ASIN:0470684143>

<ASIN:0137054890>



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.