- 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.
推薦閱讀
- Reporting with Visual Studio and Crystal Reports
- Mastering Selenium WebDriver
- 匯編語言程序設(shè)計(jì)(第2版)
- Magento 1.8 Development Cookbook
- 嚴(yán)密系統(tǒng)設(shè)計(jì):方法、趨勢(shì)與挑戰(zhàn)
- Learning JavaScript Data Structures and Algorithms
- 從零開始學(xué)Linux編程
- 動(dòng)手學(xué)數(shù)據(jù)結(jié)構(gòu)與算法
- Fast Data Processing with Spark(Second Edition)
- Swift語言實(shí)戰(zhàn)晉級(jí)
- Flask Web開發(fā):基于Python的Web應(yīng)用開發(fā)實(shí)戰(zhàn)(第2版)
- Java Hibernate Cookbook
- AI自動(dòng)化測(cè)試:技術(shù)原理、平臺(tái)搭建與工程實(shí)踐
- Flink入門與實(shí)戰(zhàn)
- WCF全面解析