Last active
March 16, 2024 21:09
-
-
Save e0ipso/8faddc9a362c31ddc9f9 to your computer and use it in GitHub Desktop.
Read only property ES6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Class that holds a read only property. | |
*/ | |
class PropertyBag { | |
/** | |
* Accessor. | |
* | |
* @return {string} | |
* The value. This annotation can be used for type hinting purposes. | |
*/ | |
static get readOnly() { | |
return 'This property can only be read.'; | |
} | |
/** | |
* Mutator. | |
* | |
* @param {*} value | |
* The passed in value. | |
* | |
* @throws {Error} | |
* Inconditionally. | |
* @return {void} | |
*/ | |
static set readOnly(value) { | |
throw new Error(`The readOnly property cannot be written. ${value} was passed.`); | |
} | |
} | |
console.log(PropertyBag.readOnly); // This property can only be read. | |
PropertyBag.readOnly = 'Ha!'; // Error: The readOnly property cannot be written. Ha! was passed. |
Anvar571
commented
Oct 25, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment