Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getters not working with inheritance #1

Open
mbabauer opened this issue Nov 16, 2015 · 0 comments
Open

getters not working with inheritance #1

mbabauer opened this issue Nov 16, 2015 · 0 comments

Comments

@mbabauer
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant