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

Static getters / setters can have incorrect this binding #4327

Closed
matthewjh opened this issue Aug 15, 2015 · 3 comments
Closed

Static getters / setters can have incorrect this binding #4327

matthewjh opened this issue Aug 15, 2015 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@matthewjh
Copy link

class Base {
  static getCorrectFoo(): any {
     return this;
  }

   static get foo(): any {
      return this;
   }
}

class Inherited extends Base {}
Base.foo === Base;
Base.getCorrectFoo() === Base;

Inherited.foo === Base; /// Should be `Inherited`
Inherited.getCorrectFoo() === Inherited;

I believe this is because the foo getter will be defined via Object.defineProperty(Base, {..}), which hard-wires this to Base within the getter, rather than binding as expected to Inherited in Inherited.foo.

@matthewjh
Copy link
Author

I have a fix in #4328, I just need some advice on testing it. Please take a look.

@DanielRosenwasser
Copy link
Member

This is actually a duplicate of #1520. See http://typescript.codeplex.com/workitem/2324 for the rationale/workaround, and as @danquirk mentions on #1868 (comment), I don't think we've changed our position on it.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Aug 16, 2015
@matthewjh
Copy link
Author

OK, so I'll (continue to) use my custom __extends so that my code functions as expected. Feels dirty as hell that I should have to do that, though.

Thanks Daniel!

@mhegazy mhegazy closed this as completed Aug 17, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants