0x000000在语法上如何作为JavaScript中的值有效?

时间:2021-12-06 06:39:38

I'm learning THREE.js and when specifying object's colours you use 0x with a hex value on the end. What does this mean?

我正在学习THREE.js,当指定对象的颜色时,你会在末尾使用带有十六进制值的0x。这是什么意思?

Are they valid variable names?

它们是有效的变量名吗?

How do they work?

他们是如何工作的?

I thought something starting with a number couldn't be a variable?

我认为以数字开头的东西不可能是一个变量?

5 个解决方案

#1


Yes, you can use hexadecimal representation for value literals. From MDN - Numeric Literal:

是的,您可以对值文字使用十六进制表示。来自MDN - Numeric Literal:

Hexadecimal

Hexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "X" (0x or 0X). If the digits after 0x are outside the range (0123456789ABCDEF), the following SyntaxError is thrown: "Identifier starts immediately after numeric literal".

十六进制数语法使用前导零,后跟小写或大写拉丁字母“X”(0x或0X)。如果0x之后的数字超出范围(0123456789ABCDEF),则抛出以下SyntaxError:“标识符在数字文字后立即启动”。

0xFFFFFFFFFFFFFFFFF // 295147905179352830000
0x123456789ABCDEF   // 81985529216486900
0XA                 // 10     

You can also find a grammar here: http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.3
(It is interesting to note that octal literal are not included here because they are not used in Strict Mode. The grammar is specified under B.1.1)

你也可以在这里找到一个语法:http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.3(有趣的是,八进制文字不包括在这里,因为它们没有被使用在严格模式中。语法在B.1.1中指定)

#2


Are they valid variable names?

它们是有效的变量名吗?

No. They are integer (number) literals. Instead of 0x000000 you could've written 0, and instead of 0x10 you could write 16.

不。它们是整数(数字)文字。您可以写0而不是0x0000而不是0x10而不是0x10。

#3


This is something javascript inherited from C and even earlier.

这是从C继承的javascript,甚至更早。

Ordinary numeric decimal constants look like this:

普通的数字十进制常量如下所示:

x = 42

Back in the old days (and even now in certain cases) it was useful to be able to specify constants in octal form; an extra leading 0 indicates this:

回到过去(甚至现在在某些情况下),能够以八进制形式指定常量是有用的;额外的前导0表示:

x = 052  /* 42 */

When hexadecimal constants started to become a useful thing, they were shoehorned in as having a leading 0x (which would have been a syntax error before):

当十六进制常量开始成为一个有用的东西时,它们被强制作为具有前导0x(之前会出现语法错误):

x = 0x2a /* 42 */

and newer versions of javascript now support binary constants (0b) and more explicit octal constants (0o):

较新版本的javascript现在支持二进制常量(0b)和更明确的八进制常量(0o):

x = 0b101010 /* 42 */
x = 0o52 /* 42 */

None of these forms are valid variable names.

这些表单都不是有效的变量名称。

#4


Each color is a value (not a variable), so:

每种颜色都是一个值(不是变量),因此:

Black is 0, which is represented in hexadecimal form as 0x000000.

黑色为0,以十六进制形式表示为0x000000。

#5


0x with a hex value on the end. What does this mean?

0x末尾带有十六进制值。这是什么意思?

These mean that they are hexadecimal numbers.

这意味着它们是十六进制数字。

Are they valid variable names?

它们是有效的变量名吗?

Which ones? If you are saying 0X then they are hexadecimal else please specify

哪个?如果您说的是0X,那么它们是十六进制的,请指定

#1


Yes, you can use hexadecimal representation for value literals. From MDN - Numeric Literal:

是的,您可以对值文字使用十六进制表示。来自MDN - Numeric Literal:

Hexadecimal

Hexadecimal number syntax uses a leading zero followed by a lowercase or uppercase Latin letter "X" (0x or 0X). If the digits after 0x are outside the range (0123456789ABCDEF), the following SyntaxError is thrown: "Identifier starts immediately after numeric literal".

十六进制数语法使用前导零,后跟小写或大写拉丁字母“X”(0x或0X)。如果0x之后的数字超出范围(0123456789ABCDEF),则抛出以下SyntaxError:“标识符在数字文字后立即启动”。

0xFFFFFFFFFFFFFFFFF // 295147905179352830000
0x123456789ABCDEF   // 81985529216486900
0XA                 // 10     

You can also find a grammar here: http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.3
(It is interesting to note that octal literal are not included here because they are not used in Strict Mode. The grammar is specified under B.1.1)

你也可以在这里找到一个语法:http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.3(有趣的是,八进制文字不包括在这里,因为它们没有被使用在严格模式中。语法在B.1.1中指定)

#2


Are they valid variable names?

它们是有效的变量名吗?

No. They are integer (number) literals. Instead of 0x000000 you could've written 0, and instead of 0x10 you could write 16.

不。它们是整数(数字)文字。您可以写0而不是0x0000而不是0x10而不是0x10。

#3


This is something javascript inherited from C and even earlier.

这是从C继承的javascript,甚至更早。

Ordinary numeric decimal constants look like this:

普通的数字十进制常量如下所示:

x = 42

Back in the old days (and even now in certain cases) it was useful to be able to specify constants in octal form; an extra leading 0 indicates this:

回到过去(甚至现在在某些情况下),能够以八进制形式指定常量是有用的;额外的前导0表示:

x = 052  /* 42 */

When hexadecimal constants started to become a useful thing, they were shoehorned in as having a leading 0x (which would have been a syntax error before):

当十六进制常量开始成为一个有用的东西时,它们被强制作为具有前导0x(之前会出现语法错误):

x = 0x2a /* 42 */

and newer versions of javascript now support binary constants (0b) and more explicit octal constants (0o):

较新版本的javascript现在支持二进制常量(0b)和更明确的八进制常量(0o):

x = 0b101010 /* 42 */
x = 0o52 /* 42 */

None of these forms are valid variable names.

这些表单都不是有效的变量名称。

#4


Each color is a value (not a variable), so:

每种颜色都是一个值(不是变量),因此:

Black is 0, which is represented in hexadecimal form as 0x000000.

黑色为0,以十六进制形式表示为0x000000。

#5


0x with a hex value on the end. What does this mean?

0x末尾带有十六进制值。这是什么意思?

These mean that they are hexadecimal numbers.

这意味着它们是十六进制数字。

Are they valid variable names?

它们是有效的变量名吗?

Which ones? If you are saying 0X then they are hexadecimal else please specify

哪个?如果您说的是0X,那么它们是十六进制的,请指定