js数组方法forEach,map,filter,every,some实现

Array.prototype.map = function(fun /*, thisp*/)
{
  var len = this.length;
  if (typeof fun != "function")
    throw new TypeError();

  var res = new Array(len);
  var thisp = arguments[1];
  for (var i = 0; i < len; i++)
  {
    if (i in this)
      res[i] = fun.call(thisp, this[i], i, this);
  }

  return res;
};

Array.prototype.filter = function(fun /*, thisp*/)
{
  var len = this.length;
  if (typeof fun != "function")
    throw new TypeError();

  var res = new Array();
  var thisp = arguments[1];
  for (var i = 0; i < len; i++)
  {
    if (i in this)
    {
      var val = this[i]; // in case fun mutates this
      if (fun.call(thisp, val, i, this))
        res.push(val);
    }
  }

  return res;
};

Array.prototype.some = function(fun /*, thisp*/)
{
  var len = this.length;
  if (typeof fun != "function")
    throw new TypeError();

  var thisp = arguments[1];
  for (var i = 0; i < len; i++)
  {
    if (i in this && fun.call(thisp, this[i], i, this))
      return true;
  }

  return false;
};

Array.prototype.every = function(fun /*, thisp*/)
{
  var len = this.length;
  if (typeof fun != "function")
    throw new TypeError();

  var thisp = arguments[1];
  for (var i = 0; i < len; i++)
  {
    if (i in this && !fun.call(thisp, this[i], i, this))
    return false;
  }

  return true;
};

Array.prototype.forEach = function(fun /*, thisp*/)
{
  var len = this.length;
  if (typeof fun != "function")
    throw new TypeError();

  var thisp = arguments[1];
  for (var i = 0; i < len; i++)
  {
    if (i in this)
      fun.call(thisp, this[i], i, this);
  }
};

Array.prototype.map = function(fun /*, thisp*/) {var len = this.length; if (typeof fun != "function") thrownewTypeError(); var res = newArray(len); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i inthis) res[i] = fun.call(thisp, this[i], i, this); } return res; }; Array.prototype.filter = function(fun /*, thisp*/) {var len = this.length; if (typeof fun != "function") thrownewTypeError(); var res = newArray(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i inthis) { var val = this[i]; // in case fun mutates thisif (fun.call(thisp, val, i, this)) res.push(val); } } return res; }; Array.prototype.some = function(fun /*, thisp*/) {var len = this.length; if (typeof fun != "function") thrownewTypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i inthis && fun.call(thisp, this[i], i, this)) returntrue; } returnfalse; }; Array.prototype.every = function(fun /*, thisp*/) {var len = this.length; if (typeof fun != "function") thrownewTypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i inthis && !fun.call(thisp, this[i], i, this)) returnfalse; } returntrue; }; Array.prototype.forEach = function(fun /*, thisp*/) {var len = this.length; if (typeof fun != "function") thrownewTypeError(); var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i inthis) fun.call(thisp, this[i], i, this); } };

原文地址:https://www.cnblogs.com/wayofeng/p/7273960.html