zoukankan      html  css  js  c++  java
  • 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); } };

  • 相关阅读:
    “main cannot be resolved or is not a field”解决方案
    .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>
    MVC学习笔记---ModelBinder
    MVC学习笔记---MVC框架执行顺序
    服务器知识----IIS架设问题
    C/C++学习笔记---primer基础知识
    C/C++学习笔记----指针的理解
    C#学习笔记-----C#枚举中的位运算权限分配
    CSS学习笔记----CSS3自定义字体图标
    Clr Via C#读书笔记----基元线程同步构造
  • 原文地址:https://www.cnblogs.com/wayofeng/p/7273960.html
Copyright © 2011-2022 走看看