6

有一个疑问,在 JS 中 Array.forEach()与 Array.map()这些方法的第三个可选参数 array...

 3 years ago
source link: https://www.v2ex.com/t/790771
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  程序员

有一个疑问,在 JS 中 Array.forEach()与 Array.map()这些方法的第三个可选参数 array 的作用

  dfkjgklfdjg · yogwang · 3 小时 13 分钟前 · 537 次点击

Rt. 一直有一个疑问,就是forEachmapfilter之类的操作数组的方法,都有一个 array 的可选参数,返回的是当前操作的数组。

但每次使用都需要 arrayData.forEach(... 来执行,所以为什么不直接在回调函数内使用一开始的 arrayData 来用呢?和第三个可选参数 array 的区别是什么?

参数

callback 为数组中每个元素执行的函数,该函数接收一至三个参数:

  • currentValue 数组中正在处理的当前元素。
  • index [可选] 数组中正在处理的当前元素的索引。
  • array [可选] forEach() 方法正在操作的数组。

thisArg 可选 可选参数。当执行回调函数 callback 时,用作 this 的值。

forEach - MDN


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK