Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript access to static child attributes

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
like image 736
Giovanni Cardamone Avatar asked Sep 21 '26 16:09

Giovanni Cardamone


1 Answers

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;
}
like image 197
NatNgs Avatar answered Sep 23 '26 04:09

NatNgs