Pure win: Array.from and Array.of

Dmitry A. Soshnikov dmitry.soshnikov at gmail.com
Sun Jul 10 10:32:49 PDT 2011


On 10.07.2011 21:18, Brendan Eich wrote:
> On Jul 10, 2011, at 9:59 AM, Dmitry A. Soshnikov wrote:
>
>> On 10.07.2011 20:36, Brendan Eich wrote:
>>> 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.
> That's all true, but beside the point. The use-case is when you can't write a literal, because you are passing a function-that-constructs as a funarg, and the eventual caller may pass only one number arg, or several args. In that case, Array will not do the right thing in the one-number-arg case.
>

You mean Function.prototype.construct 
(http://dmitrysoshnikov.com/notes/note-1-ecmascript-bound-functions/#constructor-with-various-number-of-arguments)? 
In this case spread operator will help and some additional thing for 
this is not required.

> That's the reason for Array.of.
>

Could you please show an example? From what I understand from your words 
it's sort of:

function Foo(a, b) { ... }

var object = (function (ConstructorFunction, a, b) {
   return new ConstructorFunction /* here Array.of should help some how 
to pass `a` and `b` - how? */
})(Foo, 10, 20);

Dmitry.


More information about the es-discuss mailing list