Pure win: Array.from and Array.of
brendan at mozilla.com
Sun Jul 10 09:36:50 PDT 2011
On Jul 10, 2011, at 3:06 AM, Dmitry A. Soshnikov wrote:
> 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 parens").
Note that JS's pattern for alternative constructors is String.fromCharCode, Array.from, etc. -- "class methods".
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 agree that an Array.prototype.fill or similar method is a good addition too.
More information about the es-discuss