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

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.

主站蜘蛛池模板: 潍坊市| 洪雅县| 三门峡市| 庆阳市| 开远市| 溧水县| 民权县| 黔西县| 章丘市| 龙岩市| 池州市| 安吉县| 边坝县| 商河县| 永川市| 公主岭市| 军事| 浮梁县| 潮安县| 江安县| 牡丹江市| 彭山县| 建宁县| 许昌县| 临漳县| 高要市| 天气| 通许县| 桃源县| 襄汾县| 赤水市| 苍山县| 普宁市| 江华| 阳谷县| 霍山县| 康定县| 循化| 平谷区| 江川县| 德钦县|