8
javascript forEach方法有什么用途(该地图无法做到)?
我在map和foreach中看到的唯一区别map是返回一个数组而forEach不是。但是,我什至不了解forEach方法“ func.call(scope, this[i], i, this);” 的最后一行。例如,不为“ this”和“ scope”,指的同一个对象,而不是this[i]和i参照在环路中的电流值? 我在另一篇文章中注意到有人说:“ forEach当您想根据列表的每个元素来做某事时使用。例如,您可能要在页面中添加内容。从本质上讲,当您想要“副作用”时,它非常有用。我不知道副作用是什么意思。 Array.prototype.map = function(fnc) { var a = new Array(this.length); for (var i = 0; i < this.length; i++) { a[i] = fnc(this[i]); } return a; } Array.prototype.forEach = function(func, scope) { scope = scope || this; for (var i = …