About Array.of()

Shijun He hax.sfo at gmail.com
Sun Aug 26 12:52:54 PDT 2012

On Sun, Aug 26, 2012 at 7:14 PM, Rick Waldron <waldron.rick at gmail.com> wrote:
> var o = (function( construct, ...rest ) {
>   return new construct( rest );
> })( factory [, variable arity args] );
> If factory is Array and only one numeric arg is given, inline like this:
> var o = (function( construct, ...rest ) {
>   return new construct( rest );
> })( Array, 10 );
> The result of `o` will be an array with 10 empty indexes, as if it were
> called like:
> new Array(10)
> If you replace that by using Array.of(), you avoid this "gotcha"

So you still need some code like:

if (constructor === Array) return Array.of(rest)
else return new construct(rest)

And of cause we can write it as:

if (constructor === Array) return [rest]

Or, maybe you mean we should write

o = ....(Array.of, 10)
instead of
o = ....(Array, 10)

But is this case strong enough to add such a simple function to
standard library?

> I'd argue that if code is extending ES built-ins with non-standard
> properties, it accepts the risk that one day it may be in conflict.

And if it should be added, could you choose a better name?
Array.isArray is good example which will not introduce any ambiguity.
But Array.of is not. Maybe Array.new is a good name.

Thank you!


More information about the es-discuss mailing list