1

When I declare getters in parent class, and setters in child class, getters in child classes are return undefined. I suppose this behaviour happen in favour of compiled javascript rules. In other languages (actionscript for example), you can define setters and getters in different classes, and everything work as expected. In typescript we have no warnings by typescript compiler, which is very dangerous. What reason of such behaviour?

class ImmutableVector {
    _x: number;
    _y: number;

    constructor(x: number = 0, y: number = 0) {
        this._x = x;
        this._y = y;
    }

    public get x(): number {
        return this._x;
    }

    public get y(): number {
        return this._y;
    }
}

class Vector extends ImmutableVector {
    constructor(x: number = 0, y: number = 0) {
        super(x, y);
    }

    public set x(x: number) {
        this._x = x;
    }

    public set y(y: number) {
        this._y = y;
    }
}

export default function test() {
    var immutable = new ImmutableVector(100, 100);
    console.log(immutable.x); // output: 100

    var mutable = new Vector(200, 200);
    console.log(mutable.x); // output: undefined
}
kolombet
  • 87
  • 1
  • 1
  • 7

0 Answers0