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

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.

主站蜘蛛池模板: 板桥市| 德清县| 公主岭市| 饶平县| 聂荣县| 平凉市| 明溪县| 班戈县| 加查县| 弥渡县| 沂南县| 辽阳市| 青铜峡市| 手游| 阿拉善盟| 若羌县| 万山特区| 石棉县| 宿迁市| 凤翔县| 诸城市| 侯马市| 堆龙德庆县| 红原县| 汤原县| 松溪县| 湄潭县| 绥芬河市| 正宁县| 扬州市| 兴宁市| 和静县| 酒泉市| 八宿县| 民乐县| 澄迈县| 阿克| 七台河市| 珲春市| 肥城市| 治多县|