Skip to content

Instantly share code, notes, and snippets.

@hugohenrique
Last active August 29, 2015 14:14
Show Gist options
  • Save hugohenrique/903788538b9e8250533f to your computer and use it in GitHub Desktop.
Save hugohenrique/903788538b9e8250533f to your computer and use it in GitHub Desktop.
var Environment = function () {
'use strict';
function Environment() {
this.variables = {};
}
Environment.prototype.exist = function (name) {
return !!this.variables[name];
};
Environment.prototype.add = function (name, value) {
if (!this.exist(name)) {
this.variables[name] = [];
}
this.variables[name] = value;
};
Environment.prototype.get = function (name) {
if (!this.exist(name)) {
throw new Error('The variable name cannot be found');
}
return this.variables[name];
};
Environment.prototype.replace = function (name, value) {
if (!this.exist(name)) {
throw new Error('The variable name cannot be found');
}
if (this.variables[name] !== value) {
this.variables[name] = value;
}
};
Environment.prototype.isEmpty = function (name) {
return this.get(name).length === 0;
};
return Environment;
};
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script src="Environment.js"></script>
<script>
var env = new Environment();
env.add('language', 'ptBR');
env.add('websocket.url', 'ws://my.ip.server');
var websocket = new WebSocket(env.get('websocket.url'));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment