shortcut

Constructor shortcut

A really common pattern in constructors is to use them to initialise properties via arguments you pass into the constructor, like in our example:
Copyclass Person {
    private firstName = "";
    private lastName = "";

    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
As long as you are using an access modifier TypeScript lets us shorten this to:
Copyclass Person {
    constructor(private firstName, private lastName) {
    }
}

Comments

Popular Posts