Tag Archives: EcmaScript 6

EcmaScript 6 – String.prototype.repeat()

One simple function that is on the roster to be released in EcmaScript 6 is String.prototype.repeat. The following can be used to define it in browsers which don’t currently have it defined natively:

String.prototype.repeat = String.prototype.repeat || function(count) {
    return Array(count >= 0 ? parseInt(count, 10) + 1 : -1).join(this);
};

Here are some examples of using this function (modified from the originals found on MDN):

alert("->".repeat(-1));     // RangeError
alert("->".repeat(0));      // ""
alert("->".repeat(1));      // "->"
alert("->".repeat(2));      // "->->"
alert("->".repeat(3.6));    // "->->->" (count will be converted to integer)
alert("->".repeat(1/0));    // RangeError

There is more information about this new JavaScript function here.

EcmaScript 6 – Math.sign()

One function I didn’t think would be doable in most browsers was the Math.sign. My main worry was that on the MDN page it specifies a difference between negative zero and positive zero. After doing some research I found this page which helped me realize there is a way to determine if you are dealing with normal zero or negative zero. With that in mind, my proposed definition for Math.sign is as follows:

Math.sign = Math.sign || function(n) {
  return (n = +n) ? n < 0 ? -1 : 1 : n;
};

As far as I can tell, this JavaScript function definition is a good equivalent to make the function available on browsers that dont yet natively provide this feature. Have fun! 8-)

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

EcmaScript 6 – Math.hypot()

Many know that JavaScript features vary from browser to browser. In the case of Firefox, there are many new features that have been proposed in EcmaScript 6. One of them is Math.hypot. This function adds up the squares of each argument passed and returns the square-root of that sum. In other words it could be used to find the length of the hypotenuse. Unfortunately, if you want to use this function it is not yet available in most other browsers. That is where the following snippet comes into play:

Math.hypot = Math.hypot || function() {
  var val, ret = 0, args = arguments, i = args.length;
  while (i--) {
    ret += (val = +args[i]) * val;
  }
  return Math.sqrt(ret);
};

Since there are a number of these newly proposed functions, I plan on writing a post to define a safe fallback for each one. Happy coding! 8-)