How can I access the getter superclass value in a subclass?

Accessing the value superfor getterin a derived class does not work:

class Foo {
    private _message:string = "Hello,";

    public get Message():string {
        return this._message;
    }
}

class Bar extends Foo {
    public get Message():string {
        return super.Message + " World";
    }   
}

var snafu:Bar = new Bar();
document.write(snafu.Message);

// Expected: "Hello, World"
// Actual: "undefined World"

How can I override getterand use value correctly super?

+5
source share
1 answer

I do not necessarily confirm that you are continuing this approach, but ...

class Bar extends Foo {
    public get Message():string {
        return Object.getOwnPropertyDescriptor(Foo.prototype, 'Message').get.apply(this) +  ' World';
    }   
}

Prototype inheritance does not make this particularly easy.

+6
source

All Articles