简体   繁体   English

parseInt()和octal - 哪些浏览器支持它?

[英]parseInt() and octal - which browsers support it?

The javascript parseInt function evaluates '010' as decimal 10, not octal 8, in all of the browsers I've tried - relatively recent versions of Chrome, Firefox and IE - which is what you'd expect since this has been deprecated for a long time. 在我尝试的所有浏览器中,javascript parseInt函数将'010'评估为十进制10,而不是八进制8 - 相对较新版本的Chrome,Firefox和IE - 这是你所期望的,因为这已被弃用了很久。

But I've got some code that isn't specifying the radix and I'm wondering if someone running an old browser could find a bug. 但是我有一些没有指定基数的代码,我想知道是否有人运行旧浏览器会发现错误。 Has anyone compiled a list of which browsers/versions will parse octal? 有人编译了哪些浏览器/版本将解析八进制的列表? How common is it? 有多常见?

ECMAScript 3 said ECMAScript 3

When radix is 0 or undefined and the string's number begins with a 0 digit not followed by an x or X , then the implementation may, at its discretion, interpret the number either as being octal or as being decimal. 当radix为0或未定义且字符串的数字以0开头后跟xX开头时,实现可以自行决定将数字解释为八进制或十进制。 Implementations are encouraged to interpret numbers in this case as being decimal. 鼓励实现将这种情况下的数字解释为十进制。

ECMAScript 5 says ECMAScript 5

If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X , in which case a radix of 16 is assumed. 如果radix 未定义或为0,则假定为10,除非数字以字符对0x0X 开头 ,在这种情况下假设基数为16。

Therefore, all ES5 compliant implementations will parse as decimal and not as octal. 因此,所有符合ES5的实现都将解析为十进制而不是八进制。

ECMAScript 5 forbids parsing such (unspecified radix) strings as octal. ECMAScript 5禁止将这些(未指定的基数)字符串解析为八进制。 This means IE9 and above will not have a problem (nor will most other browsers - as shown here ) 这意味着IE9及以上不会有问题(也不会对大多数其他浏览器-如图所示这里

I'd worry about IE8 in this case, as it's possibly still converting to octal given the leading zero and no radix. 在这种情况下,我担心IE8,因为它可能仍然转换为八进制,因为前导零和没有基数。 (This conversion was only discouraged, not forbidden, in ECMAScript 3 - see MDN explanation .) (在ECMAScript 3中,这种转换只是不鼓励,不是禁止的 - 请参阅MDN说明 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM