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?
Are they valid variable names?
How do they work?
I thought something starting with a number couldn't be a variable?
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?
Are they valid variable names?
How do they work?
I thought something starting with a number couldn't be a variable?
Share Improve this question edited May 6, 2015 at 21:39 ysth 98.6k6 gold badges125 silver badges217 bronze badges asked May 6, 2015 at 5:42 OliverOliver 1,6441 gold badge19 silver badges32 bronze badges 3-
1
Are you sure it's used as a variable? Looks more like a literal value (like
123
or"foo"
), something that you can assign to a variable. – Thilo Commented May 6, 2015 at 5:44 - The title says "valid as a value", but the question body says "valid variable names". Which is it? – Kobi Commented May 6, 2015 at 5:47
- 3 The question title asks if it is valid as a value and the answer is yes, but then you ask about them being valid as variable names in the body of the question, and the answer to that is no. Variables and values are very, very different. – Ray Toal Commented May 6, 2015 at 5:48
5 Answers
Reset to default 4Yes, you can use hexadecimal representation for value literals. From 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".
0xFFFFFFFFFFFFFFFFF // 295147905179352830000 0x123456789ABCDEF // 81985529216486900 0XA // 10
You can also find a grammar here: http://www.ecma-international/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)
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
.
This is something javascript inherited from C and even earlier.
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:
x = 052 /* 42 */
When hexadecimal constants started to bee a useful thing, they were shoehorned in as having a leading 0x (which would have been a syntax error before):
x = 0x2a /* 42 */
and newer versions of javascript now support binary constants (0b) and more explicit octal constants (0o):
x = 0b101010 /* 42 */
x = 0o52 /* 42 */
None of these forms are valid variable names.
Each color is a value (not a variable), so:
Black is 0, which is represented in hexadecimal form as 0x000000.
0x with a hex value on the end. What does this mean?
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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745119100a4612310.html
评论列表(0条)