Created
March 1, 2015 16:46
-
-
Save nausik/ad9d20edd185af6b6777 to your computer and use it in GitHub Desktop.
Private object var
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 obj = { | |
| one: "wat", | |
| two: "wut" | |
| }; | |
| function addPrivateVar(o, name, val) { | |
| obj[name] = (function() { | |
| var inner_val = val; | |
| return { | |
| get: function() { | |
| return inner_val; | |
| }, | |
| set: function(new_val) { | |
| inner_val = new_val; | |
| return inner_val; | |
| } | |
| } | |
| }()); | |
| } | |
| addPrivateVar(obj, "three", "wot"); | |
| addPrivateVar(obj, "four", "wat"); | |
| console.log(obj["three"].get()); | |
| console.log(obj["four"].get()); | |
| obj["four"].set("fuck"); | |
| console.log(obj["four"].get()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment