About Array.of()

Shijun He hax.sfo at gmail.com
Sun Aug 26 02:53:09 PDT 2012


I don't think Array.of() is useful, just stick on array literal seems enough:

var a = [1,2,3]
var a1 = [3]

Why we need this:

var a = Array.of(1,2,3)
var a1 = Array.of(3)

Is there any special benefit I missed?

And there is another reason why I don't like Array.of() , myself write
a small library which use Array.of(type) to get a "strong-typed"

var StringArray = Array.of(String)
var a = StringArray.from(['a', 1, true]) // ['a', '1', 'true']
a.push('string')  // ['a', '1', 'true', 'string']
a.push(100) // throws error

And Function.of() :

var sqrt = Function.of(Number).from(Math.sqrt)
sqrt(9) // 3
sqrt('9') // error
sqrt(9, 'unwanted') // error


More information about the es-discuss mailing list