const arr = [1,2,3,4,5]
Array.prototype.forEachByMe = function(callback) {
const arr = [...this];
for(let i = 0; i < arr.length; i++) {
callback(arr[i], i, arr);
console.log(i, '循环中的打印')
}
}
arr.forEachByMe((item, index) => {
if (index > 1) return;
console.log(index, '回调中的打印')
});
// 控制台打印
// 0 '回调中的打印'
// 0 '循环中的打印'
// 1 '回调中的打印'
// 1 '循环中的打印'
// 2 '循环中的打印'
// 3 '循环中的打印'
// 4 '循环中的打印'
为什么forEach循环无法被return continue打断
最新推荐文章于 2023-08-05 11:47:46 发布