admin管理员组文章数量:1126443
I ran JSLint on this JavaScript code and it said:
Problem at line 32 character 30: Missing radix parameter.
This is the code in question:
imageIndex = parseInt(id.substring(id.length - 1))-1;
What is wrong here?
I ran JSLint on this JavaScript code and it said:
Problem at line 32 character 30: Missing radix parameter.
This is the code in question:
imageIndex = parseInt(id.substring(id.length - 1))-1;
What is wrong here?
Share Improve this question edited Nov 18, 2019 at 23:23 Rimian 38.3k17 gold badges122 silver badges118 bronze badges asked Oct 19, 2011 at 9:04 Mike VierwindMike Vierwind 7,0093 gold badges17 silver badges9 bronze badges 012 Answers
Reset to default 1174It always a good practice to pass radix with parseInt -
parseInt(string, radix)
For decimal -
parseInt(id.substring(id.length - 1), 10)
If the radix parameter is omitted, JavaScript assumes the following:
- 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)
(Reference)
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.
I'm not properly answering the question but, I think it makes sense to clear why we should specify the radix.
On MDN documentation we can read that:
If radix is undefined or 0 (or absent), JavaScript assumes the following:
- [...]
- 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.
- [...]
Source: MDN parseInt()
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.
It's not recommended to do that if you don't understand this exception.
Adding the following on top of your JS file will tell JSHint to supress the radix warning:
/*jshint -W065 */
See also: http://jshint.com/docs/#options
Prior to ECMAScript 5, parseInt() also autodetected octal literals, which caused problems because many developers assumed a leading 0 would be ignored.
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);
Reference
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.
I solved it with just using the +foo, to convert the string.
Keep in mind it's not great for readability (dirty fix).
console.log( +'1' )
// 1 (int)
You can also simply add this line right above your parseInt line:
// 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.
Just put an empty string in the radix place, because parseInt() take two arguments:
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.
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.
imageIndex = parseInt(id.substring(id.length - 1))-1;
imageIndex = parseInt(id.substring(id.length - 1), '')-1;
Instead of calling the substring
function you could use .slice()
imageIndex = parseInt(id.slice(-1)) - 1;
Here, -1 in slice indicates that to start slice from the last index.
Thanks.
As the official ESLint docs state:
[...] 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.
So, when using ESLint, add radix
to the configuration:
// .eslintrc.js
module.exports = {
rules: {
radix: ['error', 'as-needed'],
},
};
本文标签: javascriptJSLint says quotmissing radix parameterquotStack Overflow
版权声明:本文标题:javascript - JSLint says "missing radix parameter" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736681001a1947422.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论