Skip to content

Instantly share code, notes, and snippets.

@mauriciosoares
Created June 4, 2014 21:34
Show Gist options
  • Save mauriciosoares/32a587979fe00fe88bca to your computer and use it in GitHub Desktop.
Save mauriciosoares/32a587979fe00fe88bca to your computer and use it in GitHub Desktop.
var constant= (function() {
var allowed = {
string: true,
number: true,
boolean: true
},
ownProp = Object.prototype.hasOwnProperty,
constants = {},
prefix,
isDefined;
prefix = (Math.random() + '_').slice(2);
isDefined = function(name) {
return ownProp.call(constants, prefix + name);
};
return {
set: function(name, value) {
if(isDefined(name)) {
return false;
}
if(!ownProp.call(allowed, typeof value)) {
return false;
}
constants[prefix + name] = value;
return true;
},
get: function() {
if(isDefined(name)) {
return constants[prefix + name];
}
return null;
},
all: function() {
return constants
}
}
} ());
constant.set('hue', 123); // true
constant.get('hue'); // 123
constant.set('hue', 456); // false
constant.get('hue'); // 123
constant.set('notAllowed', {}); // false
constant.get('notAllowed'); // null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment