3

i have this situation

import assert from 'assert'

class A {
    static x = 0

    static a () {
        return A.x
    }
}

class B extends A {
    static x = 1
}

assert.equal(B.a(), 1)

i need to retrive static value in derived class from base class in Js es6. but, i can't find a way,

the assertion will fails with

AssertionError [ERR_ASSERTION]: 0 == 1

what's the right way to do this?

  • thanks
Giovanni Cardamone
  • 167
  • 1
  • 1
  • 4

1 Answers1

1

Here, you're asking for A.x directly. You should call this.x to get A.x when you are on an object of kind A and to get B.x when you are on an object of kind B.

Just make following changes and it should work fine:

static a () {
    return this.x;
}
NatNgs
  • 874
  • 14
  • 25