February 2019
Beginner
694 pages
18h 4m
English
Classes can accept parameters during their initial construction. This allows us to combine the creation of a class and the setting of its parameters into a single line of code. Consider the following class definition:
class ClassWithConstructor {
id: number;
name: string;
constructor(_id: number, _name: string) {
this.id = _id;
this.name = _name;
}
}
Here, we have defined a class named ClassWithConstructor that has two properties, an id property of type number, and a name property of type string. It also has a constructor function that accepts two parameters. The constructor function is assigning the value of the _id argument to the class property of id, and the value of the _name argument to the class property, name. Note ...
Read now
Unlock full access