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

  • 相关阅读:
    Java8 流式 API(`java.util.stream`)
    Java8 日期与时间 API
    element-ui 开发备忘
    个人作业——软件工程实践总结作业
    【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
    团队作业第二次——项目选题报告
    结对第二次——文献摘要热词统计及进阶需求
    JAVA 之 static
    将博客搬至CSDN
    修改element ui组件样式的两种方法
  • 原文地址:https://www.cnblogs.com/wayofeng/p/7273960.html
Copyright © 2011-2022 走看看