Skip to content

Instantly share code, notes, and snippets.

@aneelkkhatri
Last active February 19, 2017 18:17
Show Gist options
  • Save aneelkkhatri/b628b8af3bc26b1afc20730f9dcfd911 to your computer and use it in GitHub Desktop.
Save aneelkkhatri/b628b8af3bc26b1afc20730f9dcfd911 to your computer and use it in GitHub Desktop.
Generic property get set adder
function setupProperty(obj, prop, setcb) {
Object.defineProperty(obj, prop, {
get: function() {
return obj['__'+prop];
},
set: function(v) {
console.log(prop);
obj['__'+prop] = v;
if (setcb) setcb(v);
}
});
}
// Use
setupProperty(obj, 'my_prop', function (v) {
// ... set sub property of v
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment