Created
June 4, 2014 21:34
-
-
Save mauriciosoares/32a587979fe00fe88bca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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