Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
scope of "this" in getters and setters of regular function
function sum() {
return this.a + this.b + this.c;
}
var o = {
a: 1,
b: 2,
c: 3,
get average() {
return (this.a + this.b + this.c) / 3;
}
};
Object.defineProperty(o, 'sum', {
get: sum, enumerable: true, configurable: true});
console.log(o.average, o.sum); // 2, 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment