By 06/07/2015 38 Comments

TypeScript Shorthand Constructor

Definition Of Constructor As “Shorthand”:

  • TypeScript has introduced the shorthand syntax of a constructor function.
  • The shorthand syntax of a constructor function allows us to declare class properties directly in the constructor as parameters along with access-modifiers.
  • When we specify properties in the constructor, the shorthand constructor syntax automatically creates these properties and these properties are called automatic properties.
  • By using a constructor shorthand technique, the automatic properties can be created.

Example:

class MarkSheet { // a class declaration

/* parameterized constructor which creates a marksheet object. Note: Different types of automatic properties (with access-modifier “private”) are directly declared in constructor as the parameters through constructor’s shorthand syntax.*/
    constructor(private studentName: string, private tsMarks: number, private aspMarks: number, private htmlMarks: number) {

    }

    /* Return type functions which returns each subject marks. Notice these functions are explicitly marked as “public”. */
    public getTsMarks(): number {
        return this.tsMarks;
    }

    public getAspMarks(): number {
        return this.aspMarks;
    }

    public getHtmlMarks(): number {
        return this.htmlMarks;
    }

    /* function declaration which returns student name. It has string as its return type, empty parameter-list and “public” access by default. */
    getName(): string {
        return (this.studentName);
    }

    /* function declaration which returns total marks. It has number as its return type, empty parameter-list and “public” access by default. */
    totalMarks(): number {
        return 300;
    }

    /* function declaration which returns obtain marks. It has number as its return type, empty parameter-list and “public" access by default. */
    obtMarks(): number {
        return (this.tsMarks + this.aspMarks + this.htmlMarks);
    }

    /* function declaration which returns percentage. It has number as its return type, 2 number type parameters and “public” access by default. */
    percentage(total: number, obtain: number): number {
        return obtain / total * 100.0;
    }
}

/* object creation of mark sheet 1, provide different kinds of information as arguments to its constructor. */
var markSheet1: MarkSheet = new MarkSheet("ABC", 85, 70, 80);

/* Following statements call the different functions of above class through its class reference (reference variable) and also print them. */
document.write("Marksheet 1: <br/>");
document.write("Student name: " + markSheet1.getName() + "<br/>");
document.write("TypeScript marks: " + markSheet1.getTsMarks() + "<br/>");
document.write("Asp marks: " + markSheet1.getAspMarks() + "<br/>");
document.write("Html marks: " + markSheet1.getHtmlMarks() + "<br/>");
document.write("Total marks: " + markSheet1.totalMarks() + "<br/>");
document.write("Obtain marks: " + markSheet1.obtMarks() + "<br/>");
document.write("Percentage: " + markSheet1.percentage(markSheet1.totalMarks(), markSheet1.obtMarks()));






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 .