Skip to content

Instantly share code, notes, and snippets.

@nausik
Created March 1, 2015 16:46
Show Gist options
  • Select an option

  • Save nausik/ad9d20edd185af6b6777 to your computer and use it in GitHub Desktop.

Select an option

Save nausik/ad9d20edd185af6b6777 to your computer and use it in GitHub Desktop.
Private object var
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