By 06/07/2015 10 Comments

Property Accessors In TypeScript Class

Definition Of TypeScript Class Property Accessors:

  • In TypeScript the concept of the property accessors is introduced by ECMA Script 5.
  • Property accessors are used to access the private properties outside from a class.
  • A property can be used as an expression as well as can be assigned to a local variable.
  • As compare with functions the property accessors are more secure, better approach and powerful for access to private properties, because we have more controls over on properties using the property accessors.
  • Property accessors have two keywords “get” and “set”, which is used with normal functions (both functions must have same name) by prefixing the keywords “get” and “set” with them.
  • The “set” accessor initializes a private property through a keyword “value” and “get” accessor returns that private property.
  • Implementation of the property accessors make object oriented programing more professional.
  • In TypeScript the “set” accessor is also called “setter” and “get” accessor is called “getter”.
  • Some browsers do not support ECMA Script 5 and throw JavaScript runtime error.
  • Access modifiers cannot be used with the property accessors in TypeScript.


class MarkSheet { // a class declaration

    /* properties declaration of different types. Notice: the following properties are marked as “private”.*/
    private _studentName: string;
    private tsMarks: number;
    private aspMarks: number;
    private htmlMarks: number;

/* parameterized constructor which has 3 parameters creates a marksheet object. */
    constructor(tsMarks: number, aspMarks: number, htmlMarks: number) {
        this.tsMarks = tsMarks;
        this.aspMarks = aspMarks;
        this.htmlMarks = htmlMarks;

/* Following are property accessors declaration “get” and “Set” which sets/initializes a private _studentName property and also return it. */
    get studentName(): string {
        return this._studentName;

    set studentName(value: string) {
        this._studentName = value;

    /* 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 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(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/>");

// Following statement calls “set” accessor automatically.
markSheet1.studentName = "Abc";

/* Following statement calls “get” accessor automatically in print statement. */
document.write("Student name: " + markSheet1.studentName + "<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 .