Basics
Challenges
Classes
Debugging
Events
External Files
Flow Control
Forms
Functions
Html Elements
Installation
Interfaces
Keywords
Modules
Namespaces
Operators
Reference Files
String
Types
Instead of referencing the class name itself (like in c#), constructors must use the constructor
keyword.
class Spaceship
{
// Properties
NumberOfEngines: number;
ShipName: string;
// constructor
constructor(engines: number, shipName: string)
{
this.NumberOfEngines = engines;
this.ShipName = shipName;
}
}
There is another way for defining a constructor
which takes incoming arguments and sets properties on the class.
See the example below.
// Example 1
class Spaceship
{
// Properties
NumberOfEngines: number;
ShipName: string;
// constructor
constructor(engines: number, shipName: string)
{
this.NumberOfEngines = engines;
this.ShipName = shipName;
}
}
// Example 2
class Spaceship
{
// This technique takes the arguments and automatically creates the arguments into properties
// of the class. Notice the access modifiers too. These are public but you can also use private, readonly, and
// protected. These are known as PARAMETER PROPERTIES.
// constructor
constructor(public NumberOfEngines: number, public ShipName: string)
{
}
}