'Any reason to use static/private static methods in TypeScript?
I'm thinking about but can't find any reason to use static methods (and especially private static) in a TypeScript class. Am I missing something? I'm asking this question because I saw code like this:
class Abc {
public someMethod() {
Abc.myMethod();
}
private static myMethod() {
...
}
}
P.S. For those who try to explain me difference between static and non-static methods and what is private method. I know these perfectly well thanks to my many years background in C#. If you read question carefully - it was about using these in TypeScript.
Solution 1:[1]
The main difference between a static method/property and a non-static one is that: at the memory level, a portion of the memory will be created for the static fields, which will be shared across all objects in the class. So it works in C # or Java.
For javascript this behavior was implemented In ES6+. But for earlier versions of Ecma Scripts typescript emulates this case.
In your case method myMethod() can be used as a way to hide the complex resource-intensive functionality of the not tied from a specific instance of the class and hidden from the end user.
See this code:
class A {
protected _p: string;
constructor() {
this._p = "A";
}
public someMethod(value: string) {
A.myMethod(this._p + value);
}
private static myMethod(p:string) {
console.log(p);
}
}
class B extends A {
constructor() {
super();
this._p = "B";
}
}
var a1 = new A();
a1.someMethod("_1");
var a2 = new A();
a2.someMethod("_2");
var b1 = new B();
b1.someMethod("_1");
Solution 2:[2]
I personally like to use private static methods to indicate a pure function. Then you absolutely know that such a method will never mutate the object's state.
Implementing functional programming whenever possible within the object oriented programming paradigm creates more side-effect free code that ultimately has lower coupling. This leads to code that is less prone to object state bugs, regression bugs, and that is generally easier to understand.
Here's a very simple example:
interface DatabaseFoo {
size: number;
color: string;
}
class Foo {
private static toDatabaseFoo(
uid: string,
color: number,
length: number,
width: number
): DatabaseFoo {
let convertedData: DatabaseFoo;
// Perform object specific data cleaning and/or transformations
return convertedData;
}
private color: number;
private length: number;
private uid: string;
private width: number;
saveToPersistence(): void {
const databaseFoo = Foo.toDatabaseFoo(
this.uid,
this.color,
this.length,
this.width
);
const jsonFoo = JSON.stringify(databaseFoo);
// Save to persistence
}
}
Solution 3:[3]
You will use private methods just within your class. It is not accessible from outside. The same as Java, etc.. Same for private static.
Static means that you want to access the method over the class name without creating an object (instantiation). It is also accessible from an outer class. Without static you need to create an object.
class Abc {
public someMethod() {
Abc.myMethod(); // Here you are able to access the static method myMethod because you are in the same class. It is not possible to access myMethod from another class directly. But someMethod() you can access directly which takes e.g. the data from myMethod();
}
private static myMethod() { // It is private and only accessible within the class and static therefore you can access it over the class name Abc.myMethod()
...
}
}
I hope it helps
Solution 4:[4]
I am not sure myself as I stumbled upon this question while searching for exactly how static members/methods will be used in TypeScript. One use case I can think of just on top of my head is something like an item renderer in a grid.
Let us say you have a renderer component class which takes in raw number and formats it with thousand separators and CCY symbols or something like that. So you have this method which takes a raw number 12345 and gives back $12,345.00
You are using this renderer on a grid on each cell so you don't want the formatter function to be created for each row. You would make it static so all instances can use this once for formatting. Then you also have something proprietary about this formatting and you don't want anyone else to use this formatting. Or just that you want to be free around making implementation changes of this method in the future and not want any other classes elsewhere in the application to use a readymade formatting because it is just lying around outside of the class. Then you make it private.
That is for the use case, am I making sense?
Having said that, I am not sure how TS would translate that in JS to retain its privateness.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | andrey.shedko |
| Solution 2 | Tom Daniel |
| Solution 3 | |
| Solution 4 | Harshal |
