How To Create Getter & Setter Methods For A TypeScript Class

Опубликовано: 29 Сентябрь 2024
на канале: IT MALAYALAM CLASSROOM
78
11

This video will explain on how to create Getter Method and Setter Method for a TypeScript Class.
Source Code:
class Person{
name:string;
constructor(name:string){
this.name = name;
}

//Getter Method for name
public getName():string{
return this.name;
}
// Setter Method for name
public setName(name:string){
this.name = name;
}
}
let p1 = new Person("Arun");
//Uses setter to change value of name
p1.setName("Kumar");
//Uses getter method to get the current value in name
console.log(p1.getName());