Reputation: 568
I have a JavaScript function which has a static variable:
function Constants() {
}
Constants.i = '1';
Now according to ECMA 6 we have const keyword. Using this we can make a variable as immutable.
I am not able to find how to use const keyword with function static variable, if I use like below it is not loading the function:
const Constant.i = '1';
It will be very helpful if anyone can suggest the proper way of doing the same.
Upvotes: 0
Views: 123
Reputation: 27174
const
only works for variables, not for object (or function) properties.
As mentioned above, you can use Object.defineProperty to define an object property that cannot be changed:
function Constants() {
}
Object.defineProperty(Constants, 'i', {
value: '1',
writable: false, // this prevents the property from being changed
enumerable: true, // this way, it shows up if you loop through the properties of Constants
configurable: false // this prevents the property from being deleted
});
Upvotes: 1