TypeScript Tutorial
TypeScript Constructors
A constructor runs when an object is created. Use it to set fields so the object starts valid.
Code that runs at creation
A constructor is a special method named constructor. The compiler calls it when you writenew Point() or new Point(3, 4). That is the moment to give fields real values, so you never print an uninitialized number.
You already created objects with new Player(). If you write no constructor, TypeScript still provides an empty one. Write your own when the object must start in a known state, or when each object needs different starting values.
A constructor with no parameters
A constructor with an empty parameter list runs for new Point(). Here it sets x andy to 0 so the point is usable at once. The fields are declared on the class; the constructor assigns them through this.
Example
class Point {
public x: number;
public y: number;
constructor() {
this.x = 0;
this.y = 0;
}
}
const origin: Point = new Point();
console.log(origin.x + " " + origin.y);Under strict, a field with no starter value must be assigned in the constructor. That is whyx and y have no = 0 on the field line: the constructor is the assignment.
Constructor with parameters
Pass values into the constructor when each object needs a different start. new Point(3, 4) calls the version that takes two numbers. You do not write p.x = 3; afterward unless you want to change it later.
Example
class Point {
public x: number;
public y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
const p: Point = new Point(3, 4);
console.log(p.x + " " + p.y);Compile this in /typescript/try. Create two points with different arguments and print both.
Parameter properties
After you can write the plain constructor, TypeScript offers a shorter form. Putpublic, private, or protected in front of a constructor parameter. That both declares the field and assigns the argument. The body can stay empty.
Example
class Point {
constructor(public x: number, public y: number) {}
}
const p: Point = new Point(3, 4);
console.log(p.x + " " + p.y);constructor(public x: number, public y: number) is the same type as the previous listing. Use the short form once the long form is clear. This tutorial still writes the long form when the constructor has extra work to do besides storing the arguments.
One constructor body
A TypeScript class has one constructor implementation. You do not write two constructor bodies the way C++ overloads constructors. To allow both new Point() and new Point(3, 4), give the parameters defaults.
Example
class Point {
public x: number;
public y: number;
constructor(x: number = 0, y: number = 0) {
this.x = x;
this.y = y;
}
}
const origin: Point = new Point();
const p: Point = new Point(3, 4);
console.log(origin.x + " " + origin.y);
console.log(p.x + " " + p.y);After you add constructor(x: number, y: number) with no defaults, the linenew Point() fails to compile. Provide defaults, or always pass the arguments.
Constructors do not return a value
Do not write return with a value in a constructor. Finish by leaving the fields set.new yields the object. Next: who is allowed to read and write those fields.