EcmaScript 6 – Array.of()

A function that is proposed to be added to EcmaScript 6 is Array.of(). This function will be used to create an array of the passed parameters. Since this function is available in some browsers but not in all, here is a block of code which when executed will define it if not already defined:

Array.of = Array.of || function() {
  return Array.prototype.slice.call(arguments);
};

Here are some examples of using the function:

var a = Array.of(1,2,3);     // [1,2,3]
var b = Array.of();          // []
var c = Array.of(undefined); // [undefined]
var d = Array.of(null);      // [null]
var e = Array.of(4);         // [4]

Not the most amusing function, but it is still a step towards EcmaScript 6. šŸ˜Ž

Leave a Reply

Your email address will not be published. Required fields are marked *