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

Ignore self binding in prefer-prototype-methods #1343

Closed
fisker opened this issue Jun 6, 2021 · 7 comments · Fixed by #1347
Closed

Ignore self binding in prefer-prototype-methods #1343

fisker opened this issue Jun 6, 2021 · 7 comments · Fixed by #1347

Comments

@fisker
Copy link
Collaborator

fisker commented Jun 6, 2021

this.foo.bind(this) and foo.bar.bind(foo) is very common.

Even though ClassName.prototype.method.bind(this) make more sense, many people still prefer the old way as they are shorter and not really "borrowing" method, should we ignore it or check under an option?

@fisker fisker changed the title Ignore prefer-prototype-methods self binding Ignore self binding in prefer-prototype-methods Jun 6, 2021
@sindresorhus
Copy link
Owner

Agreed. We can add an option if someone asks.

@EvgenyOrekhov
Copy link
Contributor

Could you clarify why does ClassName.prototype.foo.bind(this) make more sense, and why is it better than this.foo.bind(this)?
And which one is better: ClassName.prototype.foo.bind(this) or this.constructor.prototype.foo.bind(this)?

@fisker
Copy link
Collaborator Author

fisker commented Jun 8, 2021

Could you clarify why does ClassName.prototype.foo.bind(this) make more sense, and why is it better than this.foo.bind(this)?

Because .foo() is a method from ClassName.prototype, same as you don't get .push() from [] but Array.prototype.

And which one is better: ClassName.prototype.foo.bind(this) or this.constructor.prototype.foo.bind(this)?

I think first one should be prefered, but if your class don't have a name to use, you can use the second one , not much difference.

@EvgenyOrekhov
Copy link
Contributor

Hmm, this.foo.bind(this) is actually equivalent to this.constructor.prototype.foo.bind(this), NOT to ClassName.prototype.foo.bind(this).

See this example:

class Parent {
  constructor() {
    const foo = this.foo.bind(this);
    foo();
  }
  
  foo() {
    console.log("Parent foo");
  }
}

class Child extends Parent {
  foo() {
    console.log("Child foo");
  }
}

const child = new Child(); // logs "Child foo"

If you change it to const foo = Parent.prototype.foo.bind(this), it will log Parent foo, which is different from the original behavior.

But if you change it to const foo = this.constructor.prototype.foo.bind(this), it will still log Child foo, as expected.

That makes me think that this.constructor.prototype should be preferred over ClassName.prototype.

But this.constructor.prototype.foo.bind(this) looks way more wordy than this.foo.bind(this), and it even might be confusing to the ones who are familiar only with the this.foo.bind(this) syntax. So I think I would prefer to have an option to ignore self binding.

@fisker
Copy link
Collaborator Author

fisker commented Jun 9, 2021

You are right about super classes. I'm thinking maybe we should only check [] and {}, it's the initial idea of this rule #1223, we are just doing too much now. And borrowing a method from another user defined class was rarely seen in a real world case.

@fisker
Copy link
Collaborator Author

fisker commented Jun 9, 2021

People are disabling this rule https://github.com/search?q=prefer-prototype-method&type=commits

EvgenyOrekhov added a commit to EvgenyOrekhov/eslint-config-hardcore that referenced this issue Jun 9, 2021
It produces dubious warning to change `this.foo.bind(this)` to
`ClassName.prototype.foo.bind(this)`
or `this.constructor.prototype.foo.bind(this)`

See sindresorhus/eslint-plugin-unicorn#1343
@alfaproject
Copy link

Was actually going to open an issue and then saw this. I'm also disabling this rule. At the very least, an option for self binding would be nice

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

Successfully merging a pull request may close this issue.

4 participants