Basics
Challenges
Classes
Debugging
Events
External Files
Flow Control
Forms
Functions
Html Elements
Installation
Interfaces
Keywords
Modules
Namespaces
Operators
Reference Files
String
Types
In TypeScript, you cannot define an entire class as static
but you can however define it's members.
When you do this, you do not need to instantiate a new object to access those members.
When members of a class are public (not static), you access them using the this
keyword.
When members of a class are static, you call them by prefixing the call with the class name and non the
this
keyword...even within the same class.
class Logger
{
static LogInfo(message: string): void
{
console.log(message);
}
static LogError(message: string): void
{
console.error(message);
// see how we call it by using the class name instead of 'this'?
Logger.LogInfo("An error occurred...");
}
}
window.onload = function ()
{
// see how I didn't need to instantiate the class as an object?
Logger.LogInfo("hello there");
Logger.LogError("danger!");
};