I was following a tutorial on Typescript Classes and the person teaching created a class and some setter/getter methods.But when i read Typescript Documentation the approach was somewhat different. Can someone help me understand the difference between both approaches.
Approach 1:
class Student {
private _name: string;
constructor(name:string) {
this._name=name;
}
getName = (): string => {
return this._name;
}
setName = (name: string) => {
this._name = name;
}
}
Approach 2:
class Student {
private _name: string;
constructor(name:string) {
this._name=name;
}
public get name(): string {
return this._name;
}
public set name(value: string) {
this._name = value;
}
}
Have a look. In Approach 1 we write getter/setter as normal functions But in Approach 2 the keyword get/set is used. Can someone help me understand the difference between both approaches.