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); } };

  • 相关阅读:
    [ css 计数器 counter ] css中counter计数器实例演示三
    [ css 计数器 counter ] css中counter计数器实例演示二
    Leetcode 15. 三数之和
    Leetcode 13. 罗马数字转整数
    Leetcode 19. 删除链表的倒数第 N 个结点
    Leetcode 12. 整数转罗马数字
    Leetcode 11. 盛最多水的容器 双指针
    剑指 Offer 68
    剑指 Offer 68
    面试题 04.02. 最小高度树
  • 原文地址:https://www.cnblogs.com/wayofeng/p/7273960.html
Copyright © 2011-2022 走看看