Static Functions and Namespaces
Explore how TypeScript uses static functions and static properties to maintain single instances across a codebase, and understand namespaces for organizing code and preventing class name conflicts.
Static functions
A class can mark a function with the static keyword, meaning that there will only be a single instance of this function available throughout the code base.
When using a static function, we do not need to create an instance of the class in order to invoke this function, as follows:
- We have the definition of a class named
StaticFunction. This class has a single function namedprintTwoon line 4 that has been marked asstatic. This function simply prints the value2to the console.
Note: We do not need to create an instance of this class using the
newkeyword. We simply call this function by its fully qualified name, that is,<className>.<functionName>, which in this case isStaticFunction.printTwo().
Static properties
In a similar manner to static functions, classes can also have ...