TypeScript allows us to encapsulate our static methods with access modifiers, just like regular methods.
Static Methods Are Often Utility Methods They can be employed by different classes without having to create an instance, which can sometimes make the difference. Methods are created as static when object state has no effect on their behavior because they depend only on their own parameters.
Static class means is can not be instantiated, inherited from and also sealed and can not be modified. Static class in TypeScript is not natively supported but you can fake it: function Protect(target: any): void { Object. freeze(target); Object.
Generally speaking you can't access this from a static function. Static functions are called from the class scope, whereas member functions are called from object scope.
A static method exists on a class rather than an object instance. You would have to change this.getIndex
to ServerRouter.getIndex
in your init
function.
WebStorm suggests making methods static if they don't touch any of the state of an instance, since it suggests that the method exists at a level that is general to all instances of that class.
You can find out more about static
in the TypeScript Handbook (see the "Static Properties" section).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With