1

I am new to TypeScript and object oriented programming, I have some functions that should wait for each other (promises in JavaScript) and promises are depending on each other, for example:

In the main script file, I have the main class, and I imported other files and classed inside it. (I mean using import {bla-bla-class} from "bla-bla" )

I had a promise for load (depending on the page and internet speed may take some milliseconds or seconds...) using addEventListener and configSetter method is awaited for that method (load) to set the configs (I needed the page be completely loaded for setting the configs), then I needed to tell ready() method in my main class that the page is loaded and configs set correctly, because of this I added another promise for ready() method inside the main class to wait for configSetter() method (because the config setter itself needed to wait for load method, ready() was also depending on it, so I need to wait), when I compiled and bundled files, the file was heavier than what I was expecting, and also my TypeScript files was more complicated to understand because of differents promises I used

I thought maybe I can export the main class and also make its ready() method static, so I could access my main class's ready() method from another class easily, but now I am not sure, if is this something that people do in object oriented programming?

Is it okay to use the main class's methods (even as static) inside other classes and methods? (specially when they are not main classes and are just some helpful methods... should they be able to call the main class methods?)

1 Answers1

0

The main use for static functions would be to offer utility functions which do not operate on a specific instance (like the Math class for example. You do not need an instance of Math to perform math functions because that is both unnecessary use of memory and somewhat illogical).

Another use for static is sharing a common resource between multiple instances of the same class. I'll cite an example from Angular >= 2.x development:

Suppose you have a service for logging in and logging out users. Trying to keep the user profile a non-static field will lead to unexpected undefined values for the user profile in some use cases despite logging in. That is because the user profile is not shared across instances. Making it static ensures the field is one and the same for each instance of the class.

This is true for OOP in general.

Hope that helps! :)

Technohacker
  • 735
  • 5
  • 19