O'Reilly logo

Learning Angular for .NET Developers by Rajesh Gunasundaram

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Accessors

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required