constructor ts code example
Example 1: constructor in typescript
class Car {
//field
engine:string;
//constructor
constructor(engine:string) {
this.engine = engine
}
//function
disp():void {
console.log("Engine is : "+this.engine)
}
}
Example 2: classes in typescript
class Info {
private name: string ;
constructor(n:string){
this.name = n ;
};
describe(){
console.log(`Your name is ${this.name}`);
}
}
const a = new Info('joyous');
a.describe();