- Mastering TypeScript 3
- Nathan Rozentals
- 118字
- 2021-07-02 12:42:52
Static functions
Static functions are functions that can be called on a class without having to create an instance of the class first. These functions are almost global in their nature, and must be called by prefixing the function name with the class name. Consider the following class definition:
class StaticClass { static printTwo() { console.log(`2`); } } StaticClass.printTwo();
This class definition includes a single function, named printTwo, which is marked as static. As we can see from the last line of the code, we can call this function without needing to create a new instance of the StaticClass class. We can just call the function directly, as long as we prefix it with the class name.
本周热推: