While coding an app with Angular 2 and multiple calculation services I faced the following questions:
This is a snap of the class, that provides me multiple calculation methods and is instantiated on application level:
@Injectable()
export class FairnessService {
constructor(){}
private static calculateProcentValue(value: number, from: number): number {
return (Math.abs(value) / Math.abs(from)) * 100;
}
public static calculateAllocationWorth(allocation: Allocation): number {
...
}
}
Thanks for helping.
Static methods are often used to create utility functions for an application.” In other words, static methods have no access to data stored in specific objects. Note that for static methods, the this keyword references the class. You can call a static method from another static method within the same class with this.
Static methods of a class, unlike instance methods, are visible on the class itself and they are not an instance of it. Typically, when we talk about a static method, it takes input from the parameters, performs actions on it, and returns a result independently of your application.
Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.
The JavaScript allows static methods that belong to the class rather than an instance of that class. Hence, an instance is not needed to call such static methods. Static methods are called on the class directly.
They do make sense in Angular services. There are situations where we can't / don't actually need to use an instance of the service, and we can't / don't want to make a new dependency on it, we only need access to the methods our service carries. Here static members come in.
The example of using the static method defined in the service:
import { FairnessService } from './fairness.service';
export class MyComponent {
constructor() {
// This is just an example of accessing the static members of a class.
// Note we didn't inject the service, nor manually instantiate it like: let a = new A();
let value = FairnessService.calculatePercentValue(5, 50);
let value2 = FairnessService.calculatePercentValue(2, 80);
console.log(value); // => 10
console.log(value2); // => 2.5
}
}
For more information, it's explained well on: http://www.typescriptlang.org/docs/handbook/classes.html
Update
My answer before was based on a limited understanding. The static methods are available on a class itself, not on one of it's instantiations.
Here's an article that can explain this concept: https://javascript.info/static-properties-methods
seidme's answer is also solid.
Original
Static methods are represented as global variables (I think?) in the Angular application, so I think they would only be instantiated once each. Therefore I think it would not have a large impact on performance (relative to an instantiation of the class for each component that needs it.)
I use static when I don't want to inject the service and get an instance just to leverage context-agnostic formatting/utility methods. Application-wide versions of these don't seem unreasonable to me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With