radix

使用 parseInt() 时强制一致使用基数参数

💡 hasSuggestions

此规则报告的一些问题可通过编辑器建议手动修复

使用 parseInt() 函数时,通常会省略第二个参数,即基数,并让函数尝试根据第一个参数确定它是什么类型的数字。默认情况下,parseInt() 将自动检测十进制和十六进制(通过 0x 前缀)。在 ECMAScript 5 之前,parseInt() 还自动检测八进制字面,这会导致问题,因为许多开发者认为前导 0 会被忽略。

¥When using the parseInt() function it is common to omit the second argument, the radix, and let the function try to determine from the first argument what type of number it is. By default, parseInt() will autodetect decimal and hexadecimal (via 0x prefix). Prior to ECMAScript 5, parseInt() also autodetected octal literals, which caused problems because many developers assumed a leading 0 would be ignored.

这种混淆导致建议你始终使用 parseInt() 的 radix 参数来消除意外后果。所以不要这样做:

¥This confusion led to the suggestion that you always use the radix parameter to parseInt() to eliminate unintended consequences. So instead of doing this:

var num = parseInt("071");      // 57

做这个:

¥Do this:

var num = parseInt("071", 10);  // 71

ECMAScript 5 改变了 parseInt() 的行为,使其不再自动检测八进制字面,而是将它们视为十进制字面。但是,第一个参数的十六进制和十进制解释之间的差异导致许多开发者继续使用 radix 参数来确保以预期方式解释字符串。

¥ECMAScript 5 changed the behavior of parseInt() so that it no longer autodetects octal literals and instead treats them as decimal literals. However, the differences between hexadecimal and decimal interpretation of the first parameter causes many developers to continue using the radix parameter to ensure the string is interpreted in the intended way.

另一方面,如果代码仅针对符合 ES5 的环境,则传递基数 10 可能是多余的。在这种情况下,你可能希望禁止使用这样的基数。

¥On the other hand, if the code is targeting only ES5-compliant environments passing the radix 10 may be redundant. In such a case you might want to disallow using such a radix.

规则详情

¥Rule Details

此规则旨在防止将字符串意外转换为与预期不同的基数,或者如果仅针对现代环境,则防止多余的 10 基数。

¥This rule is aimed at preventing the unintended conversion of a string to a number of a different base than intended or at preventing the redundant 10 radix if targeting modern environments only.

选项

¥Options

此规则有两个选项:

¥There are two options for this rule:

  • "always" 强制提供基数(默认)

    ¥"always" enforces providing a radix (default)

  • "as-needed" 不允许提供 10 基数

    ¥"as-needed" disallows providing the 10 radix

always

默认 "always" 选项的错误代码示例:

¥Examples of incorrect code for the default "always" option:

在线运行
/*eslint radix: "error"*/

var num = parseInt("071");

var num = parseInt(someValue);

var num = parseInt("071", "abc");

var num = parseInt("071", 37);

var num = parseInt();

默认 "always" 选项的正确代码示例:

¥Examples of correct code for the default "always" option:

在线运行
/*eslint radix: "error"*/

var num = parseInt("071", 10);

var num = parseInt("071", 8);

var num = parseFloat(someValue);

as-needed

"as-needed" 选项的错误代码示例:

¥Examples of incorrect code for the "as-needed" option:

在线运行
/*eslint radix: ["error", "as-needed"]*/

var num = parseInt("071", 10);

var num = parseInt("071", "abc");

var num = parseInt();

"as-needed" 选项的正确代码示例:

¥Examples of correct code for the "as-needed" option:

在线运行
/*eslint radix: ["error", "as-needed"]*/

var num = parseInt("071");

var num = parseInt("071", 8);

var num = parseFloat(someValue);

何时不使用

¥When Not To Use It

如果你不想强制 10 基数值的存在或省略,你可以关闭此规则。

¥If you don’t want to enforce either presence or omission of the 10 radix value you can turn this rule off.

版本

此规则是在 ESLint v0.0.7 中引入。

进阶读物

资源

ESLint 中文网
粤ICP备13048890号