xdevel2000
xdevel2000

Reputation: 21364

How to create a constant in javascript

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?

Upvotes: 11

Views: 18433

Answers (5)

Jaqen H'ghar
Jaqen H'ghar

Reputation: 16804

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.

Upvotes: 3

mlegris
mlegris

Reputation: 181

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.

Upvotes: 7

kaushal
kaushal

Reputation: 815

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

Upvotes: 7

Prestaul
Prestaul

Reputation: 85145

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

Upvotes: 11

Damovisa
Damovisa

Reputation: 19423

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.

Upvotes: 2

Related Questions