Skip to content

Instantly share code, notes, and snippets.

@elisechant
Last active August 29, 2015 14:01
Show Gist options
  • Save elisechant/e5c51212af5e10e5ab09 to your computer and use it in GitHub Desktop.
Save elisechant/e5c51212af5e10e5ab09 to your computer and use it in GitHub Desktop.
Real JavaScript constants
var config = function() {
/**
* Set a debug flag
* @type {boolean}
*/
var DEBUG = true;
/**
* Set the environment variable
* @returns {string}
*/
var ENV = function() {
switch (window.location.host) {
case 'localhost':
case 'staging.domain.com':
return 'DEV';
break;
case 'domain.com':
return 'PROD';
break;
}
return false;
};
/**
* Getter for Constants
*
* So we can't access Constants directly and therefore they can't
* be overwritten.
*
* @param constant {String} - variable reference to the Constant
*/
var get = function getConstant(constant) {
return $.isFunction(constant) ? constant() : constant;
};
return {
DEBUG: get(DEBUG),
ENV: get(ENV)
};
}();
console.log(config.ENV);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment