S
S
Sergey Kotov2020-10-23 14:50:00
typescript
Sergey Kotov, 2020-10-23 14:50:00

Why are static methods in js bad?

I am writing the front in Angular. There was a task to store filter data in localStore, for this I created a class with static methods:

export class FilterStorageUtil {
  private static storage = window.localStorage;
  private static prefix = 'StorageFilter-';

  public static setFilter<T extends object>(filter: T, key = location.pathname) {
    const itemKey = this.prefix + key;
    this.storage.setItem(itemKey, JSON.stringify(filter));
    return true;
  }

  public static getFilter(key = location.pathname) {
    const itemKey = this.prefix + key;
    const jsonFilter = this.storage.getItem(itemKey);
    try {
      return JSON.parse(jsonFilter);
    } catch (e) {
      return null;
    }
  }
}


My colleague told me that this is evil and you can’t write like that, supposedly eating memory, and you need to use services. To what extent is it justified?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2020-10-23
@liqrizz

Static methods in JS are great and save memory. Static methods in your code are bad, because in this particular case it is better to use a service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question