December 2017
Beginner
372 pages
10h 32m
English
A constructor is used to initialize a new instance of a class. If we need to pass some starter values to the properties of a class at the time of object creation, we use the constructor. A constructor looks like any other function in a class with the exception of its name—it is always named as constructor. In the following example, we have a constructor of the Book class, which initializes its properties:
class Book { public author: string; public title: string; public length: number; constructor(author:string, title:string, length: number){ this.author = author; this.title = title; this.length = length;} getFullTitle(): string { return `${this.title} by ${this.author}`; }}let typeScript = new Book("Sachin Ohri","TypeScript ...
Read now
Unlock full access