One of my favorite things about JavaScript is the ability to use certain functions to do unexpected things. The following shows you the code that could be used to imitate Prototype’s corresponding function:

String.prototype.times = function(count) {
  return (new Array(count + 1)).join(this);

Basically the above code creates a new array has a length of the specified number plus one. After that, the join() function is called and the string is used as the delimiter to separate each undefined index. The way the join() function is written, undefined and null will not show up in the resulting string if they were part of the array.

Leave a Reply

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