W3C home > Mailing lists > Public > www-dom@w3.org > October to December 2011

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

From: Boris Zbarsky <bzbarsky@MIT.EDU>
Date: Fri, 23 Dec 2011 18:50:14 -0500
Message-ID: <4EF513B6.6080003@mit.edu>
To: "Marat Tanalin | tanalin.com" <mtanalin@yandex.ru>
CC: www-dom@w3.org
On 12/23/11 11:13 AM, Marat Tanalin | tanalin.com wrote:
> Actually, this is workaround use of slice() too: for making a copy of an array, we should have dedicated clone() method intended exactly to make Array copies and thus is more intuitive than slice() for this specific purpose.

This could then just be generic like other array methods, and you could 
use it to clone any arraylike.  Sounds fin to me.

>>>>   Or this object:
>>>>       { length: 2, 0: "a", 1: "b" }
>>>   Result of calling Array.prototype.slice.call() for this object is unlikely to be desired:
>>>   [a,b]
>>
>> Why is that unlikely to be desired?  That's exactly the Array object
>> that has the same behavior when iterated from 0 to length as the
>> original array.
>
> We have assotiative-array-like object with three items

What if the "length" property there were not enumerable?  Pretty simple 
to create an object like that.

> I think this is bad consequence of too much abstraction of Array-like definition based on length property existence.

It's the standard definition in ECMAScript.

>>>   Direct conversion to Array is probably just inapplicable here.
>>
>> Why not?
>
> Array is list thing while assotiative-array-like object is map. They're just not equivalent things that can be freely converted to each other lossless way.

Consider the following situations:

   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)?

>> Precisely.  But how is [a, b] really different from { length: 2, 0: "a",
>> 1: "b" } from a web-developer perspective?
>
> We actually should compare DOM list with Array, not array with assotiative-array-like object.

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.

> Generation of DOM lists is outside of web-developer control, that's why we need a convenient way to convert it to Array.

Generation of a function's input is outside that function's control by 
definition.  That's my point: it's not clear to me why from the point of 
view of the _callee_ there should be a difference between arrays and 
nodelists if you want to just make a copy of your input.

>> Sure.  What do you think
>>
>>     new Array(5)
>>
>> does?  How does that compare to
>>
>>     new Array("x")
>>
>> ?
>
> Never used such (new Array(5)) code. For Arrays, we have convenient different syntax that has more consistent and predictable behavior: [5].

You didn't answer my question.  Furthermore, the same argument applies 
to nodelists: [mylist] works fine.

-Boris
Received on Friday, 23 December 2011 23:50:57 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:14:09 GMT