theJava
theJava

Reputation: 15044

Replacing constants with some other in JavaScript

const Name = 'Name ';
const Name_USER_PASSWORD = 'Name ';
const Name= 'Name ';
const USER_FULL_NAME = 'FullName';

I have such 50 constants in my project, but i thought is there any better way to adapt this with some technique in javascript. Like putting them in prototype or something else.

Upvotes: 1

Views: 1038

Answers (2)

Rob W
Rob W

Reputation: 349222

I don't use the const keyword to declare public constants, because the global namespace would be polluted.

My recommended way of defining constants is using the Object.defineProperty method, with a fallback to a property assignment method. Additionally, I use special values for my constants: an object literal ({}). The purpose of this value is explained at the end of this answer.

// ... created a class, object, or somethin, say  MyNS
// You could write a method to implement the following constant-definition method
if (Object.defineProperty) {  // Yay, ES5!
  Object.defineProperty(MyNS, 'CONST', {
    value: {},
    enumerable: true,  // Default false. Are the props visible, eg in a for-loop?
    writable: false,   // Default false, constants should be CONSTANT
    configurable: false// Default false, prevent deletion of the constant, etc.
  });
} else { // If ES is not supported:
    MyNS.CONST = {};
}

To check against a constant:

if (setting === MyNS.CONSTANT) // .... Rest of logic

What's the advantage of this method?

Using this method, the user cannot hard-code constant values (which can lead to unexpected, hard-to-debug errors).

The comparison will always return false unless the variable is really pointing to MyNS.CONSTANT1.

An additional application is a function with a dynamic number of arguments. Say, you want to create a kind of console.log function, with configurable options. Using string/number/... constant values will lead to possible errors.
Example:

var log.CONST = 1;
function log() {
    for (var i=0; i<arguments.length; i++) {
        if (arguments[i] === log.CONST) ...some logic...
    }
}
log(1); // 1 is the value of log.CONST, the function will FAIL.
// Fix: If you were using log.CONST = {}, the assertion will only be true
//                                   when the argument === log.CONST

Upvotes: 1

nnnnnn
nnnnnn

Reputation: 150070

If you're looking for a way to organise your constants you can make them properties of one of more objects, e.g.:

var myConstants = {
       NAME               : "name",
       NAME_USER_PASSWORD : "password here",
       USER_FULL_NAME     : "Joe Jones",
       ETC                : "Other constant"
};

console.log( myConstants.USER_FULL_NAME );    // "Joe Jones"

Or a more complicated example with grouping:

var myConstants = {
       DB : {
           CONNECTION_STRING : "something",
           USER              : "name",
           PASSWORD          : "password here"
       },
       COLOR : {
           BLACK   : "#000000",
           RED     : "#FF0000",
           WHITE   : "#FFFFFF"
       }
};

console.log( myConstants.DB.PASSWORD );    // "password here"

Of course myConstants is a pretty lame name, but you get the idea...

Upvotes: 1

Related Questions