直接上代码
Array.prototype.map = function (callback, thisArg) {
let O = Object(this)
let k = 0;
let len = O.length >>> 0
let arr = [];
while (k < len) {
if (k in O) {
let item = callback.call(thisArg, O[k], k, this) // 传递给回调函数的参数
arr.push(item)
}
k++
}
return arr;
}
function parseInt(string, radix) {
// 1 0 string为1到9 radix为0 则将cur解析为十进制数据 --- 1
// 2 1 radix范围为2-36 --- NaN
// 3 2 3不能用2进制表示 --- NaN
console.log(cur, index)
...
}
let newArr = Array.prototype.map.call([1, 2, 3], parseInt); // [1,NaN,NaN]

本文详细介绍了如何手动实现JavaScript中的Array.prototype.map方法,并通过一个具体的示例展示了其使用方式。同时,深入探讨了parseInt函数的工作原理,尤其是在不同基数下的表现。这是一篇适合前端开发者深入了解JavaScript内部机制的文章。
9750

被折叠的 条评论
为什么被折叠?



