Skip to content Skip to sidebar Skip to footer

Javascript: Constant Properties

In javascript, can I declare properties of an object to be constant? Here is an example object: var XU = { Cc: Components.classes }; or function aXU() { th

Solution 1:

Since you only need it to work on the Mozilla platform, you can define a getter with no corresponding setter. The best way to do it is different for each of your examples.

In an object literal, there is a special syntax for it:

var XU = {
    getCc() { return Components.classes; }
};

In your second exampe, you can use the __defineGetter__ method to add it to either aXU.prototype or to this inside the constructor. Which way is better depends on whether the value is different for each instance of the object.

Edit: To help with the readability problem, you could write a function like defineConstant to hide the uglyness.

functiondefineConstant(obj, name, value) {
    obj.__defineGetter__(name, function() { return value; });
}

Also, if you want to throw an error if you try to assign to it, you can define a setter that just throws an Error object:

functiondefineConstant(obj, name, value) {
    obj.__defineGetter__(name, function() { return value; });
    obj.__defineSetter__(name, function() {
        thrownewError(name + " is a constant");
    });
}

If all the instances have the same value:

functionaXU() {
}

defineConstant(aXU.prototype, "Cc", Components.classes);

or, if the value depends on the object:

function aXU() {
    // Cc_value could be different for each instancevar Cc_value = return Components.classes;

    defineConstant(this, "Cc", Cc_value);
}

For more details, you can read the Mozilla Developer Center documentation.

Solution 2:

UPDATE: This works!

const FIXED_VALUE = 37;
FIXED_VALUE = 43;
alert(FIXED_VALUE);//alerts "37"

Technically I think the answer is no (Until const makes it into the wild). You can provide wrappers and such, but when it all boils down to it, you can redefine/reset the variable value at any time.

The closest I think you'll get is defining a "constant" on a "class".

// Create the classfunctionTheClass(){
}

// Create the class constantTheClass.THE_CONSTANT = 42;

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

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

// Alert the class constant from insidevar theObject = newTheClass();
theObject.alertConstant();

However, the "class" TheClass itself can be redefined later on

Solution 3:

If you are using Javascript 1.5 (in XUL for example), you can use the const keyword instead of var to declare a constant.

The problem is that it cannot be a property of an object. You can try to limit its scope by namespacing it inside a function.

(function(){ 

constXUL_CC = Components.classes;

// Use the constant here

})()

Solution 4:

To define a constant property, you could set the writable attribute to false in the defineProperty method as shown below:

Code snippet:

varXU = {};

Object.defineProperty(XU, 'Cc', {
    value: 5,
    writable: false
});

XU.Cc = 345;

console.log(XU.Cc);

Result:

5           # The value hasn't changed

Post a Comment for "Javascript: Constant Properties"