Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Rule request: no "self = this;" #2861

Closed
JoshuaKGoldberg opened this issue Jun 1, 2017 · 2 comments
Closed

Rule request: no "self = this;" #2861

JoshuaKGoldberg opened this issue Jun 1, 2017 · 2 comments

Comments

@JoshuaKGoldberg
Copy link
Contributor

A lot of older JS programs used to put var self = this; variables all over the place. We no longer need to do that now that arrow lambdas are available. Still using this is likely a symptom of not having converted over yet and/or not using captured scopes correctly.

Bad:

private act() {
    var self = this; // unnecessary
    setTimeout(
        () => {
            self.complete();
        },
        this.delay);
}

Better:

private act() {
    setTimeout(
        () => {
            this.complete();
        },
        this.delay);
}

This is in tslint-microsoft-contrib as no-var-self, though a better name might be no-self-equals-this.

@reduckted
Copy link
Contributor

Might be worth mentioning this in #1142.

@JoshuaKGoldberg
Copy link
Contributor Author

👍.

Some options for this rule that could be added in:
A. Names of allowed "self" equivalents (in the tslint-microsoft-contrib version)
B. Allow the variable if it's used in a later scope with a potentially different this.

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

No branches or pull requests

3 participants