TypeScript - Shortcut Constructor (Parameter Property)

Parameter Properties:
* Typescript offers to create a shortcut to create a constructor.
* To avoid boiler plate.

Normal Approach:
Class Customer{
//Start code
private firstName : string;
private lastName  : string;

constructor(theFirst:string, theLast:string){
this._firstName = theFirst;
this._lastName = theLast;
}
//End code

public get firstName(): string(){
return this._firstName;
}

public set firstName(value: string){
this._firstName = value;
}
}

Shortcut:
Class Customer{
//Replace Start code
constructor(private _firstName: string, private _lastName: string){
}
//Replace End code

public get firstName(): string(){
return this._firstName;
}

public set firstName(value: string){
this._firstName = value;
}
}

Use above both codes:
let myCustomer = new Customer("Vinoth", "Kumar");
myCustomer.firstName = "Raj";
console.log(myCustomer.firstName);

Comments

Popular posts from this blog

How to set Java Object into JaxBElement ?

GitLab