2

I have two classes A and B, when B extends A. See the following code

    class A {
      constructor() {
        this.dataProxy = 1
      }
     
      get data() {
        return this.dataProxy
      }
    }
    
    class B extends A {
      constructor() {
        super()
      }
      
      set data(value) {
        this.dataProxy = value
      }
    }
    
    const classB = new B()
    classB.data = 7
    console.log(classB.data)

Currently undefined is output to the console, but I want to output 7. Is there any way to do this?

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Viktor
  • 21
  • 1
  • 1
    Getters and setters are not treated independently. If a subclass defines either of them, it shadows both from the parent. See the linked question for more details. – Barmar May 20 '20 at 22:44

0 Answers0