TypeScript Hindi Tutorial #28 getter and setter

Getter and Setter In TypeScript


What is Getter in TS ?

A getter is a method that retrieves a property’s value


What is Setter in TS ?

A setter is a method that sets or updates a property’s value,


Interview Questions.


get_set.ts

class EmpInfo{
_name:string="Anil";
_email:string="anil@test.com"

get name():string{
return "MR. "+this._name;
}

set email(val:string){
this._email="emp_"+val
}
}

var emp1= new EmpInfo();

emp1.email="peter@test.com";
console.log(emp1._email);


get_set.js

"use strict";
class EmpInfo {
_name = "Anil";
_email = "anil@test.com";
get name() {
return "MR. " + this._name;
}
set email(val) {
this._email = "emp_" + val;
}
}
var emp1 = new EmpInfo();
emp1.email = "peter@test.com";
console.log(emp1._email);