Last active
December 19, 2015 13:59
-
-
Save lancevo/5966160 to your computer and use it in GitHub Desktop.
Custom object property
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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