views:

116

answers:

3

I'm designing some classes and for some data I want to use fields that are constant.

Now, AS USUAL, IE does not support the key const so

const T = 10;

not work.

I could create a property via __defineGetter__ and __defineSetter__ to mime that but , AS USUAL, IE does not support that syntax (IE 8 has Object.defineProperty but not work on custom object).

Any idea?

+1  A: 

Note that defining something as a constant really just ensures that it can't be overwritten.

If you really can't define something as a constant, what about just defining it as a variable and setting its value once. I assume you're writing the code, so you can make sure you don't overwrite it...

Not ideal obviously, but it should work.

Damovisa
A: 

Code example found here:

// Create the class
function TheClass() {
}

// Create the class constant
TheClass.THE_CONSTANT = 42;

// Create a function for TheClass to alert the constant
TheClass.prototype.alertConstant = function() {
// You can’t access it using this.THE_CONSTANT;
alert(TheClass.THE_CONSTANT);
}

// Alert the class constant from outside
alert(TheClass.THE_CONSTANT);

// Alert the class constant from inside
var theObject = new TheClass();
theObject.alertConstant();
ryanli
This creates a static variable, but it is still modifiable. I like this pattern a lot but it doesn't really provide a solution.
Prestaul
The link says that "Knowing that there’s no mechanism that prevents you from modifying your “constants”, follow that simple law : don’t modify your constants.". Maybe there's just no corresponding type with C++'s static const in JavaScript.
ryanli
+3  A: 

"No solution" is really the safest solution. Right now there is not a good mechanism for creating constants is Javascript, but if you establish a convention (ALL CAPS FOR CONSTS) to help identify them and do not overwrite them yourself then your own constants will be safe.

If you are writing a public library and you are really worried about someone modifying a value then you can use a private variable or return your constant from a function.

Use a function so the value cannot be modified:

var MY_CONSTANT = function() { return 42; }
alert(MY_CONSTANT()); // alerts "42"

Use a private value only accessible to your own code by using a closure:

(function() {
    var MY_CONSTANT = 42;
    alert(MY_CONSTANT()); // alerts "42"
    ...do other stuff in here...
})();
alert(MY_CONSTANT()); // alerts "undefined" because MY_CONSTANT is out of scope here
Prestaul
yes the only cross-browser compatible way to define a sort of constants is to use closures and provide methods to access that variable (as if it had a private variable...). But my problem was to provide developer user to get value from a constant as obj.MY_CONSTANT so your latter solution does not fit. So, at end, for that (IE also the 8 vers.) awful and incompatible browsers we developer must always find poor solution and waste our times. When M$ will understand that web developer wants STANDARDS?????
xdevel2000
I will usually not object when someone want to complain about M$ and standards, but in this case it seems unfair because javascript constants are not a part of any specification, they are Mozilla specific.
Prestaul
Yes, but I was relating to the 'standard de facto' and const, __defineGetter__ and so on are supported for e.g. by webKit engines (Chrome etc.). Also XMLHttpRequest object is not a w3C standard (it was invented by M$) but it has been adopted by all.
xdevel2000