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

Support Automatically bind this in Compiler Options #19214

Closed
ghost opened this issue Oct 16, 2017 · 6 comments
Closed

Support Automatically bind this in Compiler Options #19214

ghost opened this issue Oct 16, 2017 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@ghost
Copy link

ghost commented Oct 16, 2017

class Test {
   private myMouny:number;

  render(){
       this.myMouny = 0;
  }
}

Expected behavior:
this keyword in render always will point to Test class instance without the need to write .bind line for each function (Specially for React.js developers), and this in OOO always is the class instance, and being not is anti-pattern.

Actual behavior:
bind is not always, you need to write something like this for every function:
ooo

I heard from someone that you don't want to do this to be back-compatible with JavaScript, so I hope you insert it into a new compilerConfiguration entry (default is false also 😆 I don't mind)

Thanks for your great contribution. I love you all ❤️

@ghost ghost changed the title Suuport Automatically bind this in Compiler Options Support Automatically bind this in Compiler Options Oct 16, 2017
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 16, 2017
@RyanCavanaugh
Copy link
Member

See #3927, others. Instead of writing awkward bind calls you can just write your methods using arrow function initializers, e.g. onSubmit = () => { ... }

@ghost
Copy link
Author

ghost commented Oct 16, 2017

But I heard that arrow function evaluate non-perfomant specially when being used in react render function..

I hope I made my point correct

@ghost
Copy link
Author

ghost commented Oct 16, 2017

Beside that I can't use call super for your suggestion when using inhirtance

@ghost
Copy link

ghost commented Oct 16, 2017

Yeah, writing every method as an arrow function might be a bad idea. You might be interested in the no-unbound-method lint rule. That makes you perform the bind at the use rather than at the definition.

@RyanCavanaugh
Copy link
Member

But I heard that arrow function evaluate non-perfomant specially when being used in react render function..

This would also be the case if you automatically binded all methods.

In general you should only be having to use arrow functions in a few very specific places. Most JS libraries will give you the correct this context if used properly.

Either way, we don't change the behavior of existing JS code, so this isn't an option we'll be adding.

@ghost
Copy link
Author

ghost commented Oct 16, 2017

Thanks @RyanCavanaugh 👍

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

1 participant