views:

4593

answers:

7

If not, what's the common practice for specifying variables that are used as constants?

+4  A: 

No, not in general. FireFox implements const but I know IE doesn't.


@John points to a common naming practice for consts that has been used for years in other languages, I see no reason why you couldn't use that. Of course that doesn't mean someone will not write over the variable's value anyway. :)

Jason Bunting
As everyone knows, if IE doesn't implement it, it might as well not exist.
Josh Hinman
Unfortunately, and practically speaking - it is true. IE does own a huge share of the market. If I owned a business and had web applications used internally, I would standardize on FF. I don't know why so many people care about IE, it blows.
Jason Bunting
@JasonBunting: Your opinion != fact
Geoffrey Chetwood
@Rich: Who said my opinion was fact? You made quite the assumption. Besides, as far as I am concerned, the fact that IE sucks is a fact. You can have your own facts, I didn't say you had to believe mine. :P Take a Xanax or something...
Jason Bunting
@Rich B, yea that was just a dumb comment to make, and trust me, I would know, I make plenty of dumb comments. @Jason B. - interesting, I ran into this very problem last night.. const worked in FF but not IE. Thanks for clarification
I'm being a bit pedantic here, but it's spelled Firefox. The second F is not capitalized.
sdwilsh
LOL - yeah, since the question has nothing to do with Firefox, you are being a bit pedantic (read: ridiculous). Are you going to search this entire website for "FireFox" and correct everyone? ;)Feel free to correct the mistake yourself, no comment needed - this is a wiki after all!
Jason Bunting
+23  A: 
var MY_CONSTANT = "some-value";

Nothing's ever constant, but you can use conventions like ALL_CAPS to show that certain values should not be modified.

John Millikin
Death and taxes are constant, but I guess you meant in JavaScript...
Andrew Hedges
How about const x = 24;
Dr. Zim
+1  A: 

I use const instead of var in my Greasemonkey scripts, but it is because they will run only on Firefox...
Name convention can be indeed the way to go, too (I do both!).

PhiLho
+2  A: 

For a while, I specified "constants" (which still weren't actually constants) in object literals passed through to with() statements. I thought it was so clever. Here's an example:

with ({
    MY_CONST : 'some really important value'
}) {
    alert(MY_CONST);
}

In the past, I also have created a CONST namespace where I would put all of my constants. Again, with the overhead. Sheesh.

Now, I just do var MY_CONST = 'whatever'; to KISS.

Andrew Hedges
+15  A: 

Are you trying to protect the variables against modification? If so, then you can use a module pattern:

var CONFIG = (function() {
     var private = {
         'MY_CONST': '1',
         'ANOTHER_CONST': '2'
     };

     return {
        get: function(name) { return private[name]; }
    };
})();

alert('MY_CONST: ' + CONFIG.get('MY_CONST'));  // 1

CONFIG.MY_CONST = '2';
alert('MY_CONST: ' + CONFIG.get('MY_CONST'));  // 1

CONFIG.private.MY_CONST = '2';                 // error
alert('MY_CONST: ' + CONFIG.get('MY_CONST'));  // 1

Using this approach, the values cannot be modified. But, you have to use the get() method on CONFIG :(.

If you don't need to strictly protect the variables value, then just do as suggested and use a convention of ALL CAPS.

Burke
Note that you could just return a function for the value of CONFIG. That would save you calling CONFIG.get() all the time.
Mathew Byrne
Pretty solution. But such things should be wrapped as a library to not reinvent them in any new project.
Andrew Dashin
No, the goal was to improve maintenability. Thanks.
philippe
`CONFIG.get = someNewFunctionThatBreaksTheCode`...All in all, you absolutely cannot enforce constants in JS (w/o const keyword). Just about the only thing you can do is limit visibility.
trinithis
+7  A: 

There is the const keyword in JavaScript, but it thus far only enjoys a smattering of browser support (see the "Watch It!" sidebar).

Bill the Lizard
Your second link points to books.google.com for some reason.
Tikhon Jelvis
@Tikhon: Neither of those links pointed to the pages I originally referenced, so I just replaced them both. Thank you for pointing this out.
Bill the Lizard
If you try to assign a value to a `const`, it doesn't throw any errors. The assignment just fails and the constant still has its original value. This is a major design flaw IMHO but as long as there is a clear, consistent naming convention (such as the popular ALL_CAPS) I don't think it would cause too much grief.
MatrixFrog
+5  A: 

IE does support constants, sort of, e.g.:

<script language="VBScript">
 Const IE_CONST = True
</script>
<script type="text/javascript">
 if (typeof TEST_CONST == 'undefined') {
    const IE_CONST = false;
 }
 alert(IE_CONST);
</script>
C Nagle
Boy, talk about something that isn't cross browser . . . Still +1 for thinking a bit outside the box.
Tom