Are there any advantages to using a static modifier for functions in TypeScript?

I recently started working with TypeScript, and my WebStorm IDE has endowed me with the possibility of using a static modifier ...

export default class MyClass {
    public bar(): any {
        // do sth. with instance values
    }

    private foo(a: any, b: any): any {
        // do sth. without instance values, like checking
    }
}

Here I would receive a warning about what foo(a, b)might be announced static. At the moment, I have turned off this "warning", since I mainly consider the liberal use of statics as a "smell of code", but again I am not an expert in TypeScript.

Are there any significant “side benefits” of using a static modifier? Besides what he does in the definition .

+4
source share
3

, , static , , .

" " TypeScript , .

class Hamburger {
     static beef_percentage: number = 75;
     public tasty: boolean = false;
}

console.log(Hamburger.beef_percentage); // 75
console.log(Hamburger.tasty); // Error

var myHamburger = new Hamburger();
console.log(myHamburger.beef_percentage); // Error
console.log(myHamburger.tasty); // false

, .

, .

+5

, , ( ), , , , .

+1

If your method foodoes not depend on attributes instanceand looks more like a method utility, then it makes no sense to add it to everyone instanceand it is better to have it in one place in the classlevel

0
source

All Articles