Skip to content

Instantly share code, notes, and snippets.

@mutoo
Last active December 18, 2015 14:09
Show Gist options
  • Select an option

  • Save mutoo/5795381 to your computer and use it in GitHub Desktop.

Select an option

Save mutoo/5795381 to your computer and use it in GitHub Desktop.
a simple way to avoid memory locator detecting a number's address;
var addProperty = function(obj, property, value) {
var salt = Math.random(), saltValue;
var saltGetter = function() {
return saltValue * salt;
};
var saltSetter = function(value) {
saltValue = value / salt;
};
saltSetter(value);
Object.defineProperty(obj, property, {
get: saltGetter,
set: saltSetter
});
};
var Hero = function() {
addProperty(this, "hp", 50);
addProperty(this, "mp", 50);
};
var player = new Hero();
console.log(player.hp); // 50
console.log(player.mp); // 50
player.hp = 100;
player.mp = 75;
console.log(player.hp); // 100
console.log(player.mp); // 75
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment