Array simulation (Definition)

A means of simulating arrays in JavaScript.

With a constructor, you can simulate arrays by making them from objects and property components. This may be useful if you want to run an array based script in a very old JavaScript implementation although these days that likelihood is diminishing rapidly.

This was necessary in JavaScript version 1.0. Numbered index locations within an Object object could simulate Array objects. Named items simply allocate the next available numbered entry.

Thankfully we don't have to do this anymore.


Example code:

   // Simulate an array with an Object object
   myArray = new Object();
   myArray[0] = "One";
   myArray[1] = "Two";
   // Simulate an array with a constructor
   function SimArray(aSize)
      this.length = aSize;
      for(var index = 0; index<aSize; index++)
         this[index] = 0;
      return this;
   // Now make a simulated array
   myArray = new SimArray(12);

See also:Array(), Cast operator, Constructor function


Wrox Instant JavaScript - page - 32