English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
La classe permette l'uso di getter e setter. L'uso di getter e setter per le proprietà è saggio, specialmente se si desidera eseguire un trattamento speciale sui valori prima di restituirli o di impostarli. Per aggiungere getter e setter in una classe, utilizzare i keyword get e set.
<html> <body> <p id="method"></p> <script> class Company { constructor(brand) { this.Compname = brand; } get name() { return this.Compname; } set name(x) { this.Compname = x; } } myName = new Company("w3codebox"); document.getElementById("method").innerHTML = myName.Compname; </script> </body> </html>
输出结果
w3codebox