11

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?

5 Answers 5

11

"No solution" is really the safest solution. Right now there is not a good mechanism for creating constants in 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 only accessible to your own code by controlling scope with a function:

(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

If a developer can read a value in javascript then that developer can modify that value.

Sign up to request clarification or add additional context in comments.

3 Comments

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?????
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.
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.
7

There is a freeze() method in Javascript which can be used to create actual constants.

Usage:

    var consts = {};                    // Object to store all constants
    consts.MY_CONSTANT= 6;
    Object.freeze(consts);              // Make the consts truly read only
    consts.MY_CONSTANT= 7;              // Trying to do this will give error

Comments

7

Object.defineProperty is what you want.

For Example:

var obj = {};

Object.defineProperty(obj, 'key', {
  enumerable: false,
  configurable: false,
  writable: false,
  value: 'static'
});

Wil define "obj.key" with value : "static" and it will be readonly.

  • Enumerable means that it will show up (or not) when enumerating properties of an object.
  • Writable means if you want it to be readonly, you'd say false. not writable.
  • Configurable to false if you don't want it to be deletable from the object.

In essence, by putting them all to false, you are creating constants on the object. Btw, their defaults is to be at false.

So effectively doing this:

Object.defineProperty(obj, 'property', { value: 'value'});

Will create a constant on the 'obj' called 'property' with value 'value'. Or you could do this

function setConstant(obj, key, value)
{
    Object.defineProperty(obj, key, {value: value });
}

var obj = {};
setConstant(obj, "constantName", "constantValue");

Would make it very readable.

Comments

3

Old question but nowadays you can simply use:

const MY_CONST = 10;

From MDN:

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned.

Supported by all the main browsers already.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.