Javascript Array map(fn, context)

Description

Javascript Array map(fn, context)


Array.prototype.map = function(fn, context) {
  if (typeof fn != "function") {
    throw new TypeError(fn + " is not a function");
  }//from   w  w  w .j a  va 2 s  .c  o m

  if (typeof context === 'undefined') {
    context = this;
  }

  var result = new Array(this.length);
  for (var i = 0, l = this.length; i < l; ++i) {
    result[i] = fn.call(context, this[i], i, this);
  }

  return result;
}



PreviousNext

Related