2017-11-18 1 views
-3

マイコード:"this"を除いたメソッドを入れる場所は?

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... 
    } 
} 

私は私のコードをチェックするために、「eslint」を使用し、私はエラーがあります:

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

だから、「この」せずにメソッドを置くことを?このコードはクラスにとって重要なので、ヘルパークラスを作成しないでください。

このメソッドは重要ではないのでしょうか?他の言語のOOPでは、クラスメソッドに 'this'を含まないメソッドが存在する可能性がありますか?

+1

あなたは 'this.number(数)'とはどういう意味ですか?あなたのクラスに関数 'number()'はありませんか? – edkeveked

+0

申し訳ありませんが間違いでした。私は私の質問を更新しました。 – maspib

+3

エラーのためのドキュメントは、それをとてもうまく説明しています。https://eslint.org/docs/rules/class-methods-use-this – jmargolisvt

答えて

0

class-methods-use-thisは、クラスメソッドがこれを参照していないかどうかを調べるためのeslintチェックです。場合によっては、メソッド宣言の前にstaticキーワードを追加することで静的にするほうが良いかもしれません。それ以外の場合は、このチェックを無視することができます。

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

関連する問題