What possible use of the software could take place where a sparse array would be better than a (regular) object?
By sparse array, I mean the one where:
arr = [];
arr[0] = 'W';
arr[1] = 'T';
arr[3] = 'F';
console.log(arr[0] !== undefined)
console.log(arr[1] !== undefined)
console.log(arr[2] === undefined)
console.log(arr[3] !== undefined)
Or more formally:
An object, O, is said to be sparse if the following algorithm returns true:
1. Let len be the result of calling the [[Get]] internal method of O with argument
"length".
2. For each integer i in the range 0≤i<ToUint32(len)
a. Let elem be the result of calling the [[GetOwnProperty]] internal method of O
with argument ToString(i).
b. If elem is undefined, return true.
3. Return false.
ECMA 262 5.1 - 15.4 Array Objects
In addition, the ECMA 262 5.1 standard further defines lengthas:
The length property of this Array is a data property whose value is always numerically greater than the name of each property to delete, whose name is the index of the array.
So, the example above, arr.length === 4despite the fact that only three elements are defined.
, , Number , 3 length arr, Math.PI.
, , :
for(var i=0; i<arr.length; i++)
for(key in arr)
, Q & A , .
, Array, , , , , , , .
, Object. , Array. - ?
. , . , , .:)
, , Array , fiddle
, . .