Array.prototype.forEach()

1.语法

  • forEach()与map()差不多,但是前者是没有返回值的

2.手写

Array.prototype.forEach = function(callback,thisValue) {
  if(this == undefined) {
    throw new TypeError("this is null or not undefined!!")
  }
  if(Object.prototype.toString.call(callback) != "[object Function]") {
    throw new TypeError(callback + "is not a function!")
  }
  let _this = thisValue? thisValue : this
  for(let i = 0;i < this.length;i++) {
    callback.call(_this,this[i],i,this)
  }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • forEach() 方法对数组的每个元素执行一次提供的函数 callback为数组中每个元素执行的函数,该函数接收...
    zhangwinwin阅读 4,021评论 0 1
  • 1、JS的数据类型只有浮点型,没有整型。null,underfined,boolean,number,string...
    6e5e50574d74阅读 6,802评论 2 1
  • 对于很多刚接触java语言的初学者来说,要了解一门语言,最好的方式就是要能从基础的版本进行了解,升级的过程,以及升...
    timothyue1阅读 2,454评论 0 1
  • 第2章 基本语法 2.1 概述 基本句法和变量 语句 JavaScript程序的执行单位为行(line),也就是一...
    悟名先生阅读 9,675评论 0 13
  • 说一下JS 中的数据类型有哪些 JS 数据类型包括 基本 / 引用 / 特殊 数据类型: 1.基本数据类型:Str...
    为光pig阅读 5,272评论 0 24