- Expert Angular
- Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
- 110字
- 2021-07-15 17:05:32
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.
推薦閱讀
- Learning Java Functional Programming
- Learning RxJava
- 深入實踐Spring Boot
- RTC程序設(shè)計:實時音視頻權(quán)威指南
- 從0到1:HTML+CSS快速上手
- ASP.NET動態(tài)網(wǎng)頁設(shè)計教程(第三版)
- Mastering Scientific Computing with R
- Monitoring Elasticsearch
- ArcGIS By Example
- 深入淺出React和Redux
- Spring Boot+Vue全棧開發(fā)實戰(zhàn)
- 區(qū)塊鏈技術(shù)進(jìn)階與實戰(zhàn)(第2版)
- RESTful Web Clients:基于超媒體的可復(fù)用客戶端
- Photoshop CC移動UI設(shè)計案例教程(全彩慕課版·第2版)
- Python+Office:輕松實現(xiàn)Python辦公自動化