function _parseInt(str, radix) {
if (!radix) {
radix = 10;
}
if (!str || typeof str !== 'string' || typeof radix !== 'number' || radix < 2 || radix > 37) {
return NaN;
}
radix = radix << 0;
if (radix > 36) return NaN;
let result = NaN;
for (let i = 0; i < str.length; i ++) {
var ch = str[i];
if (ch <= '9' && ch >= '0') {
ch = ch - 0;
} else if (( ch <= 'z' && ch >= 'a') || ( ch <= 'Z' && ch >= 'A')) {
ch = str.charCodeAt(i);
if (ch > 96) {
ch = ch - 87;
} else if (ch > 64) {
ch = ch - 55;
}
} else {
return result;
}
if (ch >= radix) {
return result;
} else {
if (result != result) {
result = 0;
}
result = result * radix + ch;
}
}
return result;
}自己实现parseInt
最新推荐文章于 2022-01-05 13:06:57 发布

480

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



