官术网_书友最值得收藏!

Accessors

We can also implement getters and setters to the properties to control accessing them from the client. We can intercept a 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 shows an alert message to the effect that it is not possible.

主站蜘蛛池模板: 油尖旺区| 昭觉县| 溆浦县| 新乡市| 资中县| 贺州市| 郯城县| 乌海市| 大同县| 格尔木市| 双峰县| 青海省| 乐业县| 资溪县| 陵川县| 保靖县| 平阳县| 开远市| 高州市| 昌黎县| 琼海市| 如皋市| 南郑县| 奉贤区| 平果县| 广西| 嘉定区| 登封市| 邵东县| 历史| 吕梁市| 长宁县| 花莲市| 永靖县| 木兰县| 开原市| 张家港市| 衢州市| 昌图县| 青川县| 巴青县|