Pure win: Array.from and Array.of

Dmitry A. Soshnikov dmitry.soshnikov at gmail.com
Sun Jul 10 03:06:53 PDT 2011

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


["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 

Another thing to consider is `Array.prototype.fill` method which we 
discussed before.

The problem:

Array(4).map(function(x) x * x); // [NaN, NaN, NaN, NaN]

(by the way, this mistaken example is still mentioned in this document 
in the "Alternate Syntax Proposals" section, though, is fixed in the 
main section with `let randomArray = 
NonHoleyArray(10).map(#{Math.random()});` when was mentioned before).

The solution:

// fill with a simple value

Array(4).fill(true); // [true, true, true, true]

// fill with a function in needed context of this

let object = {data: 4};
Array(3).fill(-> if this.data > 3 { 'accepted' } else {'declined' }, 

Implementation is quite simple also, though, can be optimized in built-ins:

Array.prototype.fill || Object.defineProperty(Array.prototype, "fill", {
   valu: function arrayFill(filler) {
     for (var k = 0; k < this.length; k++) {
       this[k] = typeof filler == "function" ? filler.call(thisArg || 
this, this[k], k, this) : value;
     return this;
   configurable: true,
   writable: true

Also class-method Array.fill(count, filler) can be considered.


On 10.07.2011 9:35, Brendan Eich wrote:
> Thanks to @rwaldron for compiling these, and dherman 
> (@littlecalculist) for being a twitter conversationalist who speaks in 
> JS:
> https://gist.github.com/1074126
> http://jsfiddle.net/rwaldron/5UjWy/
> From a twitter conversation sparked by an IRC conversation where I 
> reminded jcranmer at mozilla.com <mailto:jcranmer at mozilla.com> that ES5 
> Function.prototype.bind can be used to spread arguments into a 'new' 
> expression.
> Standardizing these would save reinventing them, and the built-ins 
> could be optimized quite a bit.
> /be
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110710/0cf2bfc2/attachment.html>

More information about the es-discuss mailing list