What is the output?
const o = {
n: 1,
get d() { return this.n * 2; }
};
o.n = 5;
console.log(o.d);
- 2
- 1
- 10
- NaN
A getter runs every time the property is read, so o.d is computed from the current n: 5 * 2 = 10.
Play Problem Solving ▸
const o = {
n: 1,
get d() { return this.n * 2; }
};
o.n = 5;
console.log(o.d);