-2

My code:

class DoIt {
  city () {
    // others operations...
    const number = 5; // from operations
    const amount = this.getAmount (number);
    // others operations...
  }
  street () {
    // others operations...
    const number = 10; // from operations
    const amount = this.getAmount (number);
    // others operations...
  }
  getAmount (number) {
    return number * 10 * 3.14 / 3; // example...
  }
}

I use "eslint" to check my code and I have error:

error Expected 'this' to be used by class method 'getAmount' class-methods-use-this

So where to put a method without "this"? This code is important for my class, so I shouldn't create helper class...

Maybe this method is not important? In OOP in other languages can exists method without 'this' in class methods?

maspib
  • 13
  • 1
  • 3
  • 1
    What do you mean by `this.number(number)`? You have no function `number()` in your class? – edkeveked Nov 18 '17 at 19:19
  • Sorry, it was mistake. I updated my question. – maspib Nov 18 '17 at 19:21
  • 3
    Docs for the error explains it pretty well: https://eslint.org/docs/rules/class-methods-use-this – jmargolisvt Nov 18 '17 at 19:23
  • 2
    If it doesn't use `this`, then the method doesn't reference the object it is associated with … so why is it a method of that object? – Quentin Nov 18 '17 at 19:24
  • Possible duplicate of [How can I fix 'warning Expected 'this' to be used by class method ' eslint error?](https://stackoverflow.com/questions/47163734/how-can-i-fix-warning-expected-this-to-be-used-by-class-method-eslint-error) – Christopher Moore Aug 24 '18 at 16:34

1 Answers1

1

class-methods-use-this is an eslint check to see if your class method does not refer to this. In some cases, it may be better to make it static by adding the static keyword in front of the method declaration. In other cases, you can just ignore this check.

https://eslint.org/docs/rules/class-methods-use-this

mweiss
  • 1,363
  • 7
  • 14