Re: toArray() method for array-like DOM list objects

On 12/26/11 6:20 PM, Marat Tanalin | tanalin.com wrote:
>>     var obj1 = new Array();
>>     obj1[0] = "x";
>>     obj1["z"] = "y";
>>
>>     var obj2 = new Object();
>>     obj2[0] = "x";
>>     obj2["z"] = "y";
>>     Object.defineProperty(obj2, "length",
>>                           { value: 2, writable: true,
>>                             configurable: true, enumerable: false });
>>
>> What are the exact differences between obj1 and obj2 as long as you
>> don't plan to modify them (so you're just reading data you were passed)?
>
> Using Array object (instead of pure Object) as associative array is usually considered bad practice because Array's length property does not reflect items with nonnumeric keys.

Neither does the "length" property of the Object above.

Seriously, I think you're trying to read in distinctions that are not 
quite there.

>> I think you have a pretty confused understanding of how Arrays work in
>> ECMAScript.  They're just another associative-array-like object whose
>> prototype happens to be Array.prototype, and with some special behavior
>> around changes to the "length" property.
>
> That "some special behavior" does matter, I think.

Why, for cases when you only plan to read the array or object or 
whatever, not modify it?

-Boris

Received on Tuesday, 27 December 2011 00:00:05 UTC