Javascript 为什么在严格模式下不允许使用八进制数字文字(以及解决方法是什么?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34358331/
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
Why are Octal numeric literals not allowed in strict mode (and what is the workaround?)
提问by xameeramir
Why are Octal numeric literals not allowed in JavaScript strict mode? What is the harm?
为什么在JavaScript 严格模式下不允许使用八进制数字文字?有什么危害?
"use strict";
var x = 010; //Uncaught SyntaxError: Octal literals are not allowed in strict mode.
<h1>Check browser console for errors</h1>
In case a developer needsto use Octals(which can mistakenly change a numbers meaning), is there a workaround?
采纳答案by Amit
The "why" part of the question is not really answerable.
问题的“为什么”部分并不是真正可以回答的。
As for "how", off the top of my head...
至于“如何”,在我的脑海中......
"use strict";
var x = parseInt('010', 8);
document.write(x);
回答by Patrick Roberts
Octal literals are not allowed because disallowing them discourages programmers from using leading zeros as padding in a script. For example, look at the following snippet:
八进制文字是不允许的,因为禁止它们会阻止程序员在脚本中使用前导零作为填充。例如,查看以下代码段:
var eight = 0008,
nine = 00009,
ten = 000010,
eleven = 011;
console.log(eight, nine, ten, eleven);
Seems harmless enough, right? We programmers with OCD just want to align all the commas together so it looks nicer. But here's the problem:
看起来足够无害,对吧?我们有强迫症的程序员只想把所有的逗号对齐,这样看起来更好看。但问题来了:
8 9 8 9
This is the output. See how inconsistent it becomes? Not all zero-padded numeric literals will convert to octal, since 8 and 9 are not octal digits. It's harder to keep them consistent when having to remember all these rules, so strict mode
makes it easier by disallowing it altogether.
这是输出。看看它变得多么不一致?并非所有填充零的数字文字都会转换为八进制,因为 8 和 9 不是八进制数字。当必须记住所有这些规则时,很难保持它们的一致性,因此strict mode
通过完全禁止它会更容易。
Instead you should pad with leading spaces, or if you want to use octal, then utilize parseInt()
with the optional radix
argument of 8
to specify octal.
相反,您应该填充前导空格,或者如果您想使用八进制,则使用parseInt()
可选radix
参数 of8
来指定八进制。
Here are the two "solutions", respectively:
这里分别是两个“解决方案”:
"use strict";
var eight = 8,
nine = 9,
ten = 10,
eleven = 11;
console.log(eight, nine, ten, eleven);
"use strict";
var eight = parseInt('010', 8),
nine = parseInt('011', 8),
ten = parseInt('012', 8),
eleven = parseInt('013', 8);
console.log(eight, nine, ten, eleven);
回答by Willian Balmant
Nowadays, with large browser support to ES6, you could write this:
现在,有了对 ES6 的大量浏览器支持,你可以这样写:
const NINE = 0o11; // octal
const TEN = 0b1010; // binary
const SEVENTEEN = 0x11; // hexa
回答by gsnedders
Why is Octal numeric literals not allowed in javascript strict mode? What is the harm?
为什么在javascript严格模式下不允许八进制数字文字?有什么危害?
Octals in JS have historically been a non-standard extension to the standard (in ES5, which introduces strict mode, they're in Annex B, which is a collection of non-standard features that most implementations support: except it defines octals in a way incompatible with what websites require), and strict mode made an attempt to disallow all non-standard extensions. The "why" as to why they were never standardised is an obvious related question, and that I'm unaware of.
JS 中的八进制历来是标准的非标准扩展(在引入严格模式的 ES5 中,它们在附件 B 中,这是大多数实现支持的非标准特性的集合:除了它在一个方式与网站要求不兼容),并且严格模式试图禁止所有非标准扩展。关于为什么它们从未标准化的“为什么”是一个明显的相关问题,我不知道。
In case a developer need to use Octals (which can mistakenly change a number's meaning), is there a work around?
如果开发人员需要使用八进制(这可能会错误地改变数字的含义),是否有解决方法?
As @Amit answered, parseInt
with its second argument as 8 still works in strict mode.
正如@Amit 回答的那样,parseInt
它的第二个参数为 8 仍然在严格模式下工作。