TFrazee
TFrazee

Reputation: 807

Javascript (ES8) - Get static value of class from parent class

I have a class Parent (defined using the class declaration, though I'm aware this is primarily syntactic sugar), and multiple classes (Child1, Child2, etc) that extend it. The child classes have a static property assigned to them (outside of their declarations - as far as I'm aware there is no way to assign a static property within a class declaration).

I would like to access the a static value of any child class from the parent class, such as in the method getStaticValue().

class Parent {
    constructor() {
        //Do parent stuff
    }

    getStaticValue() {
        return "The value of staticValue is " + this.staticValue;
    }
}

class Child1 extends Parent {
    constructor() {
        super();
        //Do child1 stuff
    }
}
Child1.staticValue = "Child1";

class Child2 extends Parent {
    constructor() {
        super();
        //Do child2 stuff
    }
}
Child2.staticValue = "Child2";

I want to access the value of staticValue of any arbitrary child from the parent class, however attempting to do so as written above always returns undefined. In other words:

let parentStaticValue = new Parent().getStaticValue();
//Desired output = "The value of staticValue is undefined"
//Actual output = "The value of staticValue is undefined"

let child1StaticValue = new Child1().getStaticValue();
//Desired output = "The value of staticValue is Child1"
//Actual output = "The value of staticValue is undefined"

let child2StaticValue = new Child2().getStaticValue();
//Desired output = "The value of staticValue is Child2"
//Actual output = "The value of staticValue is undefined"

Is there a way to access the a static value of a child class from the parent class, without having to know the name of the child class in every case?

Upvotes: 4

Views: 1991

Answers (2)

traktor
traktor

Reputation: 19376

You can use the constructor property of a class instance to access static properties held as properties of the instance's constructor function, as in:

class Parent {
    constructor() {
        //Do parent stuff
    }

    getStaticValue() {
        return this.constructor.staticValue;
    }
}

Warning

The constructor property of an object is inherited from the prototype chain.

Class syntax made the prototype object property of a class constructor's function object non-configurable (good), but left the constructor property of this same prototype object writable (bad).

NEVER change the constructor value of class constructors' prototype objects if you know what's good for you want class extensions to work correctly.

Demo:

class Parent {
    constructor() {
        //Do parent stuff
    }

    getStaticValue() {
        return "The value of staticValue is " + this.constructor.staticValue;
    }
}

class Child1 extends Parent {
    constructor() {
        super();
        //Do child1 stuff
    }
}
Child1.staticValue = "Child1";

class Child2 extends Parent {
    constructor() {
        super();
        //Do child2 stuff
    }
}
Child2.staticValue = "Child2";

console.log(new Parent().getStaticValue()); 
console.log(new Child1().getStaticValue());
console.log(new Child2().getStaticValue());

Upvotes: 4

semanser
semanser

Reputation: 2358

You can pass your static value to the parent constructor using super() in a child class:

class Parent {
    constructor(childStaticValue) { // receive the value from the children class
        this.staticValue = childStaticValue // and assign it to the local variable
    }

    getStaticValue() {
        return "The value of staticValue is " + this.staticValue;
    }
}

class Child1 extends Parent {
    constructor() {
        super(Child1.staticValue); // pass the value to the parent class
        //Do child1 stuff
    }
}
Child1.staticValue = "Child1";

class Child2 extends Parent {
    constructor() {
        super(Child2.staticValue); // pass the value to the parent class
        //Do child2 stuff
    }
}
Child2.staticValue = "Child2";

Upvotes: 2

Related Questions