15.4 Array objects

2010-07-04

Unlike most languages, arrays are basically just hashmaps. They are objects optimized for fast access to numbered properties.

A property P is an "array index" if and only if ToString(ToUInt32(P)) === P and ToUInt32(P) is not equal to (2^32)-1 (because that denotes NaN). In other words, any 32bit unsigned integer (except for one) can be a valid array index, when converted to string.

The most distinguishable property of arrays is length. You cannot set it to a negative number. Setting it will increase or decrease the number of elements in the array. Setting an "array index" to something equal or larger than the current value of length will increase length to be one higher than the array index. The value is always a unsigned (positive) 32bit integer less than 2^32.

Whenever you change length the array will automatically delete any array index with a value equal or greater than the new value of length.

The length and array indices only apply to own property. Changing an inherited length property does nothing for the array that inherits it.

An object is "sparse" if NOT ALL array indices below its length value exist as an OWN property. We use the following algorithm to determine this.

Code: (Meta Ecma)
function IsSparse(O){
var len = O.[[Get]]("length");
len = ToUint32(len);
for (var i=0; i return false;
}



Object Array.[[Prototype]] = Function.prototype
int Array.length = 1
boolean Array.isArray()
string Array.prototype.toString()
string Array.prototype.toLocaleString()
Array Array.prototype.concat([item1:string|Array [, item2:string|Array [, ... ]]])
string Array.prototype.join(separator:string=',')
mixed Array.prototype.pop()
int Array.prototype.push([item1:mixed [, item2:mixed [, ... ]]])
Array Array.prototype.reverse()
mixed Array.prototype.shift()
Array Array.prototype.slice(start:int, end:int)
undefined Array.prototype.sort(comparefn:Function)
Array Array.prototype.splice(start:int, deleteCount:int [, item1:mixed [, item2:mixed [, ... ] ] ] )
int Array.prototype.unshift([item1:mixed [, item2:mixed [, ... ]]])
int Array.prototype.indexOf(searchElement:mixed [, fromIndex:int ])
int Array.prototype.lastIndexOf(searchElement:mixed [, fromIndex:int ])
boolean Array.prototype.every(callbackfn:function [, thisArg:mixed ])
boolean Array.prototype.some(callbackfn:function [, thisArg:mixed ])
undefined Array.prototype.forEach(callbackfn:function [, thisArg:mixed ])
Array Array.prototype.map(callbackfn:function [, thisArg:mixed ])
Array Array.prototype.filter(callbackfn:function [, thisArg:mixed ])
mixed Array.prototype.reduce(callbackfn:function [, initialValue:mixed ])
mixed Array.prototype.reduceRight(callbackfn:function [, initialValue:mixed ])