We can also implement getters and setters to the properties to control accessing them from the client. We can intercept some process before setting a value to a property variable or before getting a value of the property variable:
var updateCustomerNameAllowed = true;
Class Customer {
Private _name: string;
get name: string {
return this._name;
}
set name(newName: string) {
if (updateCustomerNameAllowed == true) {
this._name = newName;
}
else {
alert("Error: Updating Customer name not allowed!");
}
}
}
Here, the setter for the name property ensures that the customer name can be updated. Otherwise, it will show an alert message that it is not possible.