meteor / ecmascript-runtime

Polyfills for new ECMAScript 2015 APIs like Map and Set
3 stars 4 forks source link

getters not working with inheritance #1

Open mbabauer opened 9 years ago

mbabauer commented 9 years ago

This is most likely an issue w/ Babel, and may be related to this issue, but I wanted to post this to see if someone might be able to help me.

Say I have the following classes defined:

A = class A {
    constructor(value, options) {
        this.value = value;
        this.options = options;
        console.log('constructor this: ', this);
    }

    get value() {
        return this._value;
    }

    set value(value) {
        this._value = value;
    }

    get options() {
        return this._options;
    }

    set options(options) {
        this._options = options;
    }

    get validationErrors() {
        // Do standard validations for all classes of type A here
        console.log('A.validationErrors this: ', this);
        var validationErrs = [];
        if (options && options.required && !value) {
            validationErrs.push('Value is required');
        }

        return validationErrs;
    }
}

B = class B extends A {
    get validationErrors() {
        console.log('B.validationErrors this: ', this);
        var valErrs = super.validationErrors;
        // Do custom validations for class B here
        return valErrs;
    }
}

var b = new B('val', {required: true});
var valErrs = b.validationErrors;

Strangely, what I see in the logs is:

constructor this: B{_value: "val", _options: {required: true}}
B.validationErrors this: B{_value: "val", _options: {requried: true}}
A.validationErrors this: A{}

Furthermore, if I call this.value or this.options in the parent's validationErrors, I end up with undefined returned. It seems like the constructor and getters are being copied down, at least in behavior, but they shouldn't be, right? I would think that the child's constructor would simply call the parent's in this case, but that doesn't seem to be happening.