javascript lint 警告:parseInt 缺少基数参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17681843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:11:11  来源:igfitidea点击:

lint warning: parseInt missing radix parameter

javascriptjslint

提问by user1723894

I have the following code that gives the warning described in the title:

我有以下代码给出了标题中描述的警告:

          year: parseInt(dateParts[0]),
               ......................^

Any help is much appreciated

任何帮助深表感谢

回答by Quentin

See the manual for parseInt; it takes 2 arguments. The second one tells it which number base you want to use. This is almost always going to be 10(decimal).

参见手册parseInt;它需要 2 个参数。第二个告诉它你想使用哪个数基。这几乎总是10(十进制)。

parseInt(dateParts[0],10)

If you don't specify it, then it will be inferred from the data.

如果你不指定它,那么它将从数据中推断出来。

If radix is undefined or 0 (or absent), JavaScript assumes the following:

If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.

If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

If the input string begins with any other value, the radix is 10 (decimal).

如果 radix 未定义或为 0(或不存在),JavaScript 会假设如下:

如果输入字符串以“0x”或“0X”开头,则基数为 16(十六进制)并解析字符串的其余部分。

如果输入字符串以“0”开头,则基数为八(八进制)或 10(十进制)。究竟选择哪个基数取决于实现。 ECMAScript 5 指定使用 10(十进制),但并非所有浏览器都支持它。由于这个原因,在使用 parseInt 时总是指定一个基数。

如果输入字符串以任何其他值开头,则基数为 10(十进制)。