LoginSignup
3
2

More than 5 years have passed since last update.

静的メソッドの呼び出し(class構文)

Posted at

ESlintのclass-methods-use-thisルールを有効にしている場合、
thisを使用しないメソッドに関しては静的メソッドを定義する必要がありまして、
static MyMethod()としていないと、次のようなエラーになります。

terminal
error  Expected 'this' to be used by class method 'MyMethod'  class-methods-use-this
js
class MyClass {
    constructor(alpha) {
        this.alpha = alpha
    }

    MyMethod () {
        console.log('This is bad')
    }
}

MyClass.callStaticMethod() と記述することで、
補助メソッド的に他のメソッドで呼び出すことが可能です。

js
class MyClass {
    constructor(alpha) {
        this.alpha = alpha
    }

    PrimaryMethod () {
        MyClass.StaticMethod()
        console.log(this.alpha)
    }

    StaticMethod() {
        console.log('This is good')
    }
}
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2