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

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 http://wiki.ecmascript.org/doku.php?id=strawman:shorter_function_syntax, 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' }, object);

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.

Dmitry.

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 [email protected] <mailto:[email protected]> 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
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to