Last active
August 29, 2015 14:14
-
-
Save hugohenrique/903788538b9e8250533f 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 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; | |
}; |
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
<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