问答题1197/1593实现 Array.prototype.forEach()

难度:
2021-07-06 创建

参考答案:

1Array.prototype.forEach = function(callback, thisArg) { 2 if (this == null) { 3 throw new TypeError('this is null or not defined'); 4 } 5 if (typeof callback !== "function") { 6 throw new TypeError(callback + ' is not a function'); 7 } 8 const O = Object(this); 9 const len = O.length >>> 0; 10 let k = 0; 11 while (k < len) { 12 if (k in O) { 13 callback.call(thisArg, O[k], k, O); 14 } 15 k++; 16 } 17}

最近更新时间:2021-07-07

赞赏支持

预览

题库维护不易,您的支持就是我们最大的动力!