I'm looking at the source code of a C++ project similar to one that I am doing in Javascript for reference.
In C++, I have
#define FIRST_THING 0x0001
#define SECOND_THING 0x0002
...
What do these values mean? And how would I define this in Javascript? Things break if I try to just use 0x0001 and such, so could I just do
var FIRST_THING = 1
var SECOND_THING = 2
or is that pletely different?
I'm looking at the source code of a C++ project similar to one that I am doing in Javascript for reference.
In C++, I have
#define FIRST_THING 0x0001
#define SECOND_THING 0x0002
...
What do these values mean? And how would I define this in Javascript? Things break if I try to just use 0x0001 and such, so could I just do
var FIRST_THING = 1
var SECOND_THING = 2
or is that pletely different?
Share Improve this question asked Feb 1, 2012 at 19:33 KalinaKalina 5,59418 gold badges67 silver badges104 bronze badges 11-
1
var FIRST_THING = 0x000001;
works fine – qwertymk Commented Feb 1, 2012 at 19:34 - 1 There is nothing similar between your C++ project and your Javascript project. Ever. – Lightness Races in Orbit Commented Feb 1, 2012 at 19:36
- Yes. You say "thinks break", but what things? Care to give an example? – Mr Lister Commented Feb 1, 2012 at 19:36
- Define "things break" because, well, they don't. – Lightness Races in Orbit Commented Feb 1, 2012 at 19:36
- 1 upvoted as to not discourage the OP. This is obviously a beginner, she is confused, and the question is valid (if perhaps trivial to most of us, but we all start somewhere). I realize it's a bit unclear, but again, she's confused. – Ed Swangren Commented Feb 1, 2012 at 19:45
3 Answers
Reset to default 50x0001
is an integral constant in base 16, i.e., hexadecimal. It is still 1
in base 10. So yes, your example is equivalent, but do you know how to mentally parse 0xBC
? If not then you need to study up on arbitrary base arithmetic or at least get fortable with hex as any programmer should know this stuff.
Sometimes it is easier to view numbers in hex form as they represent bit patterns. In hex, two digits correspond to a byte, so you know at a glance that 0xFF
is 255
base 10 and 11111111
base 2. Work on some lower level projects for a while and it will bee second nature.
In your C++ example the integral constants are textually replaced by the preprocessor (i.e., all occurrences of FIRST_THING
are replaced by 0x0001
before the code is piled), you don't have such a tool in javascript, so just assign the values to variables directly.
You cannot create 'constants' in javascript, so it's up to you to make sure that you don't change them. However, you can simply write
firstThing = 0x0001;
And it will work just as the C++ example does, i.e., firstThing
takes on the value of 1
.
In the C++, those are preprocessor constants in hexadecimal.
In javascript, there is no preprocessor.
have a look at Are there constants in JavaScript?
There aren't any constants, but convention can help.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745484313a4629697.html
评论列表(0条)