Skip to content

Instantly share code, notes, and snippets.

@Omkaragrawal
Created May 18, 2020 22:21
Show Gist options
  • Save Omkaragrawal/cd6b694aecfaec330dd050e01235ed61 to your computer and use it in GitHub Desktop.
Save Omkaragrawal/cd6b694aecfaec330dd050e01235ed61 to your computer and use it in GitHub Desktop.
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