Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bilalucar
Created April 17, 2017 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bilalucar/4db6ddca3eb259045c609d045beec083 to your computer and use it in GitHub Desktop.
Save bilalucar/4db6ddca3eb259045c609d045beec083 to your computer and use it in GitHub Desktop.
JS Overwrite prevention
/* Overwrite prevention */
var obj = {
foo: 'this is the value of foo'
};
Object.defineProperties(obj, {
'getFoo': {
value: function () {
return this.foo;
}
},
'setFoo': {
value: function (val) {
this.foo = val;
}
}
});
obj.getFoo = 66;
// getFoo is not going to be overwritten!
console.log(obj.getFoo());
// "this is the value of foo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment