- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Private Fields
JavaScript Classes
JavaScript Private Fields
A field starting with a hash is private and cannot be read from outside the class.
The underscore convention was only ever a hint; nothing enforced it.
A # field is genuinely inaccessible from outside.
Example
Example
javascript
class Account {
#balance = 100;
getBalance() {
return this.#balance;
}
}
console.log(new Account().getBalance());