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

  • 相关阅读:
    mac的webdriver自动化
    MongoDB win安装后无法远程连接访问
    Fiddler的一些坑: !SecureClientPipeDirect failed: System.IO.IOException
    Mac终端用Sublime打开指定文件或文件夹
    [Spring常见问题]java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    Flask_Flask-Mail邮件扩展(十三)
    Flask_Flask-Migrate数据迁移扩展(十二)
    Flask + flask_sqlalchemy + jq 完成书籍展示、新增、删除功能
    Flask_CSRF保护(十一)
    SQLAlchemy(十)
  • 原文地址:https://www.cnblogs.com/wayofeng/p/7273960.html
Copyright © 2011-2022 走看看