- 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.
推薦閱讀
- Spring技術內幕:深入解析Spring架構與設計
- Learning ArcGIS Pro
- Access 2016數據庫管
- 名師講壇:Spring實戰開發(Redis+SpringDataJPA+SpringMVC+SpringSecurity)
- Gradle for Android
- Python算法從菜鳥到達人
- MATLAB for Machine Learning
- Python機器學習算法: 原理、實現與案例
- NoSQL數據庫原理
- SQL Server 2008中文版項目教程(第3版)
- Continuous Delivery and DevOps:A Quickstart Guide Second Edition
- Visual Basic語言程序設計基礎(第3版)
- Mastering Magento Theme Design
- MATLAB從入門到精通
- Unity AI Game Programming(Second Edition)