You can do this with any object. Actually, this has nothing to do with the fact that it is an Array-like object.
var myObj = {}; myObj[0] = 'some value'; alert( myObj[0] );
Here is an example of creating an object like Array:
Example: http://jsfiddle.net/8rgRM/
// create constructor function MyObj(){} // extend it with an Array method MyObj.prototype.push = Array.prototype.push; // create an instance var inst = new MyObj; // use the push method inst.push( 'some value' ); // the instance automatically has a length property that was updated alert( inst.length );
It may seem tempting that the length property will behave the same as in a real array, but it is not.
For example, with an array, you can do length = 0 to clear the contents of the array, but it will not work in an object like the above.
In the jQuery source, here is a link to Array.prototype.push and here where it is extended in the jQuery prototype.
user113716
source share