Skip to content

Instantly share code, notes, and snippets.

@lancevo
Last active December 19, 2015 13:59
Show Gist options
  • Save lancevo/5966160 to your computer and use it in GitHub Desktop.
Save lancevo/5966160 to your computer and use it in GitHub Desktop.
Custom object property
var obj = {
x : 5,
y : 10
};
// defineProperty doesn't work on IE < 9, IE8 only when it's a DOM element
Object.defineProperty(obj, 'sum', {
get: function(){
return this.x + this.y
}
});
console.log(obj.sum === 15)
obj.x = 1;
obj.y = 3;
console.log(obj.sum == 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment