Skip to content

do not error on 'super' property access in ES6 #5860

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Dec 11, 2015
Merged

Conversation

vladima
Copy link
Contributor

@vladima vladima commented Dec 1, 2015

fixes #338. #4465

@mhegazy
Copy link
Contributor

mhegazy commented Dec 1, 2015

so how dose this work with non-accessor properties?

@vladima
Copy link
Contributor Author

vladima commented Dec 1, 2015

@mhegazy ES6 natively support it so there is no error. However results might be quite unexpected. Consider this example:

'use strict'

class A {}
Object.defineProperty(A.prototype, "x", { value: 1 });
class A1 extends A {
    method() { return super.x; }
}

console.log(new A1().x);        // 1
console.log(new A1().method()); // 1

class B {
    constructor() {
        this.x = 2;
    }
}

class B1 extends B {
    method() { return super.x; }
}

console.log(new B1().x);        // 2
console.log(new B1().method()); // undefined

Reason is: super call inside new B1() will set x on this meaning that it will be own property for B1 but not for B so attempt to read it in method will return undefined.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 1, 2015

yup. if you put it on the prototype it is going to work, except that all instances share the same variable, which is not expected, if you do not, super.x does not really point to any thing, as super and this are really the same object. that is the reason we did not allow it in the past. the question is should we do it now? we should talk about this in the design meeting.

@vladima
Copy link
Contributor Author

vladima commented Dec 1, 2015

AFAIR main reason why we did not do this downlevel was: super access to data and accessor properties is different so we'll need to emit helper code to avoid runtime errors. For ES6 helpers are not necessary and the question is mostly: do we want to block potentially legitimate user scenarios just because we don't have way to know if super property access will use data or accessor property.

@vladima
Copy link
Contributor Author

vladima commented Dec 11, 2015

pinging @RyanCavanaugh, can you pls confirm that this PR is approved on design meeting before I check it in?

@RyanCavanaugh
Copy link
Member

👍

vladima added a commit that referenced this pull request Dec 11, 2015
do not error on 'super' property access in ES6
@vladima vladima merged commit f51de5b into master Dec 11, 2015
@vladima vladima deleted the superPropertiesInES6 branch December 11, 2015 21:20
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow to call getter/setter logic When subclassing
4 participants