Это было бы намного более чистым подходом
var CONSTANTS = function() {
var constants = { } ; //Initialize Global Space Here
return {
defineConstant: function(name,value)
{
if(constants[name])
{
throw "Redeclaration of constant Not Allowed";
}
},
getValue(name)
{
return constants[name];
}
} ;
}() ;
CONSTANTS.defineConstant('FOO','bar') ;
console.log(CONSTANTS.getValue('FOO')) ; //Returns bar
CONSTANTS.defineConstant('FOO','xyz') ; // throws exception as constant already defined
CONSTANTS.getValue('XYZ') ; //returns undefined