const result = ['1', '3', '10'].map(parseInt); // 这⾥会打印出什么呢? console.log( result );

const result = ['1', '3', '10'].map(parseInt);
// 这⾥会打印出什么呢? 
console.log( result );

作答

打印结果是[1, NaN, 2] 因为map的参数是

function(current, index, arr) { // 当前元素值,当前元素索引值,数组本身
}

parseInt的参数是:

parseInt(str, radix) // 解析的字符串,⼏进制(若省略或为0,则以10进⾏解析,若⼩于2或者⼤于36,则返回NaN)

所以该题展开来写:

const result = ['1', '3', '10'].map(function(cur, index, arr) {
return parseInt(cur, index);
});
// 执⾏过程:
// parseInt('1', 0) -> 1
// parseInt('3', 1) -> NaN
// parseInt('10', 2) -> 2
作者:Roc
链接:https://juejin.cn/post/6970867290480853006
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 
虚心学习、丰富自己
原文地址:https://www.cnblogs.com/tkqq000/p/14868047.html