Pure win: Array.from and Array.of

Dmitry A. Soshnikov dmitry.soshnikov at gmail.com
Sun Jul 10 09:59:31 PDT 2011


On 10.07.2011 20:36, Brendan Eich wrote:
> On Jul 10, 2011, at 3:06 AM, Dmitry A. Soshnikov wrote:
>
>> Array.from is a good addition, I guess any good framework has it.
>>
>> Though, `Array.of` in contrast doesn't bring much of a sugar. Compare these two apples-to-apples:
>>
>> Array.of( "things", "that", "aren't", "currently", "an", "array" )
>>
>> vs.
>>
>> ["things", "that", "aren't", "currently", "an", "array"]
>>
>> what's the goal in first case to write this useless "Array.of" prefix and exactly the same to manually enumerate the items? In fact, the second one is more suggared than the first one (the first one is: "added useless prefix Array.of and brackets around items are replaced with call parens").
> Note that JS's pattern for alternative constructors is String.fromCharCode, Array.from, etc. -- "class methods".
>

Yes, that's OK with `Array.from`. That's said, many libs have such a 
util. Some langs, e.g. Ruby just expose such a possibility as instance 
method. That is, an object there can just call its inherited method 
`to_a` to convert `self` to array value. Though, `Array.from(...)` seems 
better for JS for the same reason as `Object.defineProperty` is placed 
on `Object` instead of the `Object.prototype`.

> So the goal of Array.of is to provide a constructor that, unlike Array, does not have that insane special case for Array(42), which presets length (and hints to implementations to preallocate) but leaves holes in [0, length).
>

I still don't see how it will help in manual enumeration of the same 
items which may be directly passed to brackets of array initialiser. We 
enumerate (by hands) items here, right? -- Array.of(1, 2, 3). And we 
enumerate items here (by hands also) -- [1, 2, 3]. The difference is 
that the second case syntactically more elegant and sugared and also 
doesn't require non-needed function activation with allocating 
call-stack frame, etc. Still `Array.of` seems just useless for me. 
Correct me if I'm wrong. But I'm all in for the `Array.from`.

Dmitry.


More information about the es-discuss mailing list