About Array.of()

Rick Waldron waldron.rick at gmail.com
Sun Aug 26 13:03:24 PDT 2012



On Sunday, August 26, 2012 at 3:52 PM, Shijun He wrote:

> 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.
> 
> 

Array.of is unambiguous with the current ES specification 

Rick
 
> 
> 
> Thank you!
> 
> --
> hax
> 
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120826/58df6b28/attachment.html>


More information about the es-discuss mailing list