Back to questions
JuniorTheoryCommon
What does the program print?
class Rect {
constructor(w, h) {
this.w = w;
this.h = h;
}
get area() {
return this.w * this.h;
}
}
const r = new Rect(2, 3);
r.w = 5;
console.log(r.area);15
- Expecting
6: a getter is not computed once when the object is created; it runs on every read, sor.areauses the neww—5 * 3. - Expecting
undefinedbecauseareais read without(): agetmethod runs on a plain property read, andr.area()would in fact stop the program with aTypeError.
- What does the program print if
r.w = 5;is moved below theconsole.logline? - What happens if the program calls
r.area()with parentheses?