语法错误:不推荐使用以 "0" 为前缀的八进制文字

JavaScript 严格模式 特有的异常“0 前缀的八进制文字已弃用;使用 "0°" 前缀代替”发生在使用弃用的八进制文字(0 后跟数字)时。

¥The JavaScript strict mode-only exception "0-prefixed octal literals are deprecated; use the "0o" prefix instead" occurs when deprecated octal literals (0 followed by digits) are used.

信息

¥Message

SyntaxError: Octal literals are not allowed in strict mode. (V8-based)
SyntaxError: Decimals with leading zeros are not allowed in strict mode. (V8-based)
SyntaxError: Unexpected number (V8-based)
SyntaxError: "0"-prefixed octal literals are deprecated; use the "0o" prefix instead (Firefox)
SyntaxError: Decimal integer literals with a leading zero are forbidden in strict mode (Safari)

错误类型

¥Error type

仅限 严格模式 中的 SyntaxError

¥SyntaxError in strict mode only.

什么地方出了错?

¥What went wrong?

八进制文字已弃用。当你在十进制整数前面加上 0 时,你实际上会将其更改为八进制文字,这可能令人惊讶。标准化语法使用前导零,后跟小写或大写拉丁字母 "O"(0o0O)。

¥Octal literals are deprecated. When you prefix a decimal integer with 0, you actually change it to an octal literal, which may be surprising. The standardized syntax uses a leading zero followed by a lowercase or uppercase Latin letter "O" (0o or 0O).

前导零始终是被禁止的,即使文字不是有效的八进制文字语法(例如文字包含数字 89,或者它有小数点)。数字文字只能以 0 开头,如果 0 是其单位位。

¥Leading zeros are always forbidden, even when the literal is not valid octal literal syntax (such as when the literal contains the digits 8 or 9, or it has a decimal point). A number literal may only start with 0 if that 0 is its units place.

示例

¥Examples

"0" 前缀的八进制文字

¥"0"-prefixed octal literals

js
"use strict";

03;

// SyntaxError: "0"-prefixed octal literals are deprecated; use the "0o" prefix instead

有效的八进制数

¥Valid octal numbers

使用前导零后跟字母 "o" 或 "O":

¥Use a leading zero followed by the letter "o" or "O":

js
0o3;

也可以看看

¥See also