ANGULAR in less than 128 words – TypeScrip – Angular Part 7

This article follows the first six on the subject ANGULAR and deals with the language TypeScript:

Manufacturer:

The builder is the method called to create the instance of an object

Class Point
    x: number;
    y: number;

    constructor (x: number, y: number)
        this.x x;
        this.y - y;
    }
    add (point: Point)
        return new Point (this.x - point.x, this.y - point.y);
    }
}

var p1 - new Point (30, 5);
var p2 - new Point (14, 21);
var p3 - p1.add (p2);

Optional setting:

If a setting is declared optional then all the parameters declared to its right are optional. Example of the name setting in the manufacturer.

Class Point
    x: number;
    y: number;
    name: string;

    constructor (x: number, y: number, name?:string)
        this.x x;
        this.y - y;
        this.name - name;
    }
}

Visibility:

By default the visibility of the setting is public, you can use "access modifiers" to change it.

Class Point
    private x: number;

Access modifiers can be positioned on methods, variables and properties.

Class Point
...
     private add (point: Point)
        return new Point (this.x - point.x, this.y - point.y);
    }
}
Class Point
...
    constructor (private x: number, private y: number)
...

Adding an access modifier (public/private/protected/reading only) to a builder setting will automatically assign that setting to a field of the same name.

Getter and setter:

TypeScript supports getters/setters as a way to intercept access to a member of an object.

This allows for a finer control over how a member is accessed on each object.

Const lengthMaxDuNom - 10;

Wage class
    private _nomComplet: string;

    get nameComplet(): thong
        return this._nomComplet;
    }

    name setComplet (newName: string)
        if (newName - newNom.length - lengthMaxDuNom)
            throw new Error ("Error Maximum length of the name reached, maximum length allowed - " lengthMaxDuNom);
        }
        
        this._nomComplet - newName;
    }
}

let salarie - new Salarie();
salarie.nameComplet - "Toto Hello";
if (employee.nomComplete)
    console.log (employee.nameComplete);
}

References to read:

Retour en haut