By 06/07/2015 18 Comments

Create Constructor In TypeScript Class

The Class Constructor:

  • It is a special type of function which has no return type even void.
  • It can be declared with a “constructor” keyword, a constructor might be declared as public access modifier but it is as optional. It is called from outside the class.
  • A constructor is used to initialize the properties, an object and performs any task which creates a fully formed object OR it causes the initialization of require components for an object to be fully created. Here the word component means (properties, variables or any other class member etc. OR another words: It is the full responsibility of a constructor that it creates an object by giving require values to its attributes and behavior.
  • A constructor is called automatically when we create an object of a class OR It is automatically called after new operator at the time of an object creation.
  • In TypeScript, multiple constructors are not allowed in a class.
  • Like functions one or more parameters (local variables) can be passed in a constructor which is called parametrized constructor. If no parameter is passed in constructor that is called: a construct with an empty parameter-list.
  • All classes have a constructors by default, if we do not define our custom/own constructor the TypeScript compiler supplies a default constructor which sets the all properties to its default values such as “undefined”.

Syntax Of A Class Constructor:

Access-modifier constructor (parameterList){
   Object components / member variables / properties to be initialized
} 

Access-modifier

  • Must be a keyword “public” (optional).

constructor

  • A constructor declaration indication.

(parameterList)

  • List of local variables (optional).

{

  • Starting scope of constructor.

 Object components / member variables / properties to be initialized

  • That can be properties.

}

  • Ending scope of constructor.

Example – 1, A Mark sheet Program Describes An Example Of A Constructor With Empty Parameter-List:

class MarkSheet { // declare a class MarkSheet, start its scope.

/* Notice, in this example we do not initialize class fields in this class, we just declared these fields. */
    studentName: string;
    address: string;
    tsMarks: number;
    aspMarks: number;

 /* Following is a declaration of a constructor with empty parameters, it initializes four properties or initialize a Marksheet object with values. */
    constructor() {
        this.studentName = "Abc";
        this.address = "Xyz";
        this.tsMarks = 90;
        this.aspMarks = 80;
    }

} // End class definition scope.

/* Class constructor is automatically called when Marksheet object or instance is created, NOTICE: class constructor is called after new keyword as MarkSheet() and it has no any argument. */
var markSheet: MarkSheet = new MarkSheet();

/* Following statements print the different kind of information of a Mark sheet by using the reference variable (markSheet) of type MarkSheet class followed by dot operator (.), dot operator links a class member with a reference variable. */
document.write("Mark sheet object: <br/>");

document.write("Student name: " + markSheet.studentName + "<br/>");
document.write("Address: " + markSheet.address + "<br/>");
document.write("TypeScript marks: " + markSheet.tsMarks + "<br/>");
document.write("Asp.net marks: " + markSheet.aspMarks);

Example – 2, A Mark Sheet Program Describes An Example Of Parametrized Constructor:

class MarkSheet { // declare a class MarkSheet, start its scope.

/* Notice, in this example we do not initialize class fields in this class, we just declared these fields. */
    studentName: string;
    address: string;
    tsMarks: number;
    aspMarks: number;

/* Following is a declaration of a parametrized constructor with four parameters, it initializes four properties or initialize a Mark sheet object with values. */
    constructor(studentName: string, address: string, tsMarks: number, aspMarks: number) {
        this.studentName = studentName;
        this.address = address;
        this.tsMarks = tsMarks;
        this.aspMarks = aspMarks;
    }

} // End class definition scope.

/* Class constructor is automatically called when Marksheet object or instance is created, NOTICE: class constructor is called after new keyword as MarkSheet() and also four arguments are being passed to the constructor. */
var markSheet: MarkSheet = new MarkSheet("Abc", "Xyz", 95, 80);

/* Following statements print the different kind of information of a Mark sheet by using the reference variable (markSheet) of type MarkSheet class followed by dot operator (.), dot operator links a class member with a reference variable. */
document.write("Mark sheet object: <br/>");

document.write("Student name: " + markSheet.studentName + "<br/>");
document.write("Address: " + markSheet.address + "<br/>");
document.write("TypeScript marks: " + markSheet.tsMarks + "<br/>");
document.write("Asp.net marks: " + markSheet.aspMarks);






Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .