Javascript JSLint 说“缺少基数参数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7818903/
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
JSLint says "missing radix parameter"
提问by Mike Vierwind
I ran JSLint on this JavaScript code and it said:
我在这段 JavaScript 代码上运行了 JSLint,它说:
Problem at line 32 character 30: Missing radix parameter.
第 32 行第 30 行的问题:缺少基数参数。
This is the code in question:
这是有问题的代码:
imageIndex = parseInt(id.substring(id.length - 1))-1;
What is wrong here?
这里有什么问题?
回答by Jayendra
It always a good practice to pass radix with parseInt -
使用 parseInt 传递基数总是一个好习惯 -
parseInt(string, radix)
For decimal -
对于十进制 -
parseInt(id.substring(id.length - 1), 10)
If the radix parameter is omitted, JavaScript assumes the following:
如果 radix 参数被省略,JavaScript 假定如下:
- If the string begins with "0x", the radix is 16 (hexadecimal)
- If the string begins with "0", the radix is 8 (octal). This feature is deprecated
- If the string begins with any other value, the radix is 10 (decimal)
- 如果字符串以“0x”开头,则基数为16(十六进制)
- 如果字符串以“0”开头,则基数为 8(八进制)。此功能已弃用
- 如果字符串以任何其他值开头,则基数为 10(十进制)
(参考)
回答by Zanon
To avoid this warning, instead of using:
为避免此警告,请不要使用:
parseInt("999", 10);
You may replace it by:
您可以将其替换为:
Number("999");
Note that parseInt and Number have different behaviors, but in some cases, one can replace the other.
请注意, parseInt 和 Number 具有不同的行为,但在某些情况下,一个可以替换另一个。
回答by nmoliveira
I'm not properly answering the question but, I think it makes sense to clear why we should specify the radix.
我没有正确回答这个问题,但我认为明确为什么我们应该指定 radix是有道理的。
On MDN documentation we can read that:
在 MDN 文档中,我们可以读到:
If radix is undefined or 0 (or absent), JavaScript assumes the following:
如果 radix 未定义或为 0(或不存在),则 JavaScript 假定如下:
- [...]
- 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.
- [...]
- [...]
- 如果输入字符串以“0”开头,则基数为八(八进制)或 10(十进制)。究竟选择哪个基数取决于实现。 ECMAScript 5 指定使用 10(十进制),但并非所有浏览器都支持它。由于这个原因,在使用 parseInt 时总是指定一个基数。
- [...]
Source: MDN parseInt()
回答by Spock
You can turn off this rule if you wish to skip that test.
如果您想跳过该测试,您可以关闭此规则。
Insert:
插入:
radix: false
Under the "rules
" property in the tslint.json
file.
在文件中的“ rules
”属性下tslint.json
。
It's not recommended to do that if you don't understand this exception.
如果您不了解此异常,则不建议这样做。
回答by aleemb
Adding the following on top of your JS file will tell JSHint to supress the radix warning:
在您的 JS 文件顶部添加以下内容将告诉 JSHint 抑制基数警告:
/*jshint -W065 */
See also: http://jshint.com/docs/#options
回答by user2369834
I solved it with just using the +foo, to convert the string.
我只使用 +foo 来转换字符串就解决了这个问题。
Keep in mind it's not great for readability (dirty fix).
请记住,它的可读性不好(脏修复)。
console.log( +'1' )
// 1 (int)
回答by Rohit Nethi
You can also simply add this line right above your parseInt line:
您也可以简单地在 parseInt 行正上方添加此行:
// eslint-disable-next-line
This will disable eslint check for the next line. Use this if you only need to skip one or two lines.
这将禁用下一行的 eslint 检查。如果您只需要跳过一两行,请使用此选项。
回答by Ahmed.Dz
Just put an empty string in the radix place, because parseInt() take two arguments:
只需在基数位置放一个空字符串,因为 parseInt() 有两个参数:
parseInt(string, radix);
parseInt(string, radix);
string The value to parse. If the string argument is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string argument is ignored.
字符串 要解析的值。如果字符串参数不是字符串,则将其转换为字符串(使用 ToString 抽象操作)。字符串参数中的前导空格将被忽略。
radix An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above-mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.
radix 一个介于 2 和 36 之间的整数,表示上述字符串的基数(数学数字系统中的基数)。为人类常用的十进制数字系统指定 10。始终指定此参数以消除读者混淆并保证可预测的行为。当未指定基数时,不同的实现会产生不同的结果,通常默认值为 10。
imageIndex = parseInt(id.substring(id.length - 1))-1;imageIndex = parseInt(id.substring(id.length - 1), '')-1;
imageIndex = parseInt(id.substring(id.length - 1))-1;imageIndex = parseInt(id.substring(id.length - 1), '')-1;
回答by Goran_Ilic_Ilke
Simply add your custom rule in .eslintrc which looks like that
"radix": "off"
and you will be free of this eslint unnesesery warning.
This is for the eslint linter.
只需在 .eslintrc 中添加您的自定义规则,它看起来像这样
"radix": "off"
,您将摆脱这个 eslint unnesesery 警告。这是用于 eslint linter。
回答by SanTom
Prior to ECMAScript 5, parseInt() also autodetected octal literals, which caused problems because many developers assumed a leading 0 would be ignored.
在 ECMAScript 5 之前, parseInt() 还自动检测八进制文字,这会导致问题,因为许多开发人员认为前导 0 将被忽略。
So Instead of :
所以而不是:
var num = parseInt("071"); // 57
Do this:
做这个:
var num = parseInt("071", 10); // 71
var num = parseInt("071", 8);
var num = parseFloat(someValue);