inherit getter setter
2022年5月1日小于 1 分钟
inherit getter setter
Question
What does the code snippet to the right output by console.log
?
Snippet
let val = 0
class A {
set foo(_val) {
val = _val
}
get foo() {
return val
}
}
class B extends A { }
class C extends A {
get foo() {
return val
}
}
const b = new B()
console.log(b.foo)
b.foo = 1
console.log(b.foo)
const c = new C()
console.log(c.foo)
c.foo = 2
console.log(c.foo)
console.log(b.foo)