Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Methods and Angular 2 Services in JavaScript ES6

While coding an app with Angular 2 and multiple calculation services I faced the following questions:

  1. When do I use static in a Angular service provided on application level? Is that nonsense?
  2. How does a static method reflect on performance? Lets say a couple hundret objects call at the same time the same static method. Is this method instantiated more than once?

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.

like image 508
Ore Avatar asked Jan 25 '17 20:01

Ore


People also ask

What is static method in es6?

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.

What is static service in angular?

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.

What is static method in JavaScript?

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.

Does JavaScript support static methods?

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.


2 Answers

  1. Static methods of a class, unlike instance methods, belong to (are visible on) the class itself (not an instance of it). They do not depend on the instance members of a class and will usually take input from the parameters, perform actions on it, and return some result. They act independently.

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
    }
}
  1. Static methods have no impact on the performance. As we've ascertained above, they do not depend on any instance of the class, and invoking those methods will in no way instantiate the class.

For more information, it's explained well on: http://www.typescriptlang.org/docs/handbook/classes.html

like image 117
seidme Avatar answered Oct 22 '22 18:10

seidme


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.

like image 42
chrispy Avatar answered Oct 22 '22 17:10

chrispy