A JavaScript class can keep state or behavior inaccessible to outside code by prefixing a member name with #. Unlike an underscore naming convention, this is enforced by the language: private members can be accessed only from methods defined on the class. section 5.6 section 5.6.1
The boundary has observable consequences:
- Private members are not inherited and do not appear in property enumeration or ordinary property-descriptor queries. section 5.6.1
- Access depends on the matching object as `this`; separating a method from that object and calling it directly fails. section 5.6.1
class SecureDrawer {
#pinCode;
constructor(pin) {
this.#pinCode = pin;
}
#validatePin(pin) {
return this.#pinCode === pin;
}
unlock(pin) {
if (!this.#validatePin(pin)) {
throw new Error('access denied');
}
}
}
const secureDrawer = new SecureDrawer(12345);
console.log(Object.keys(secureDrawer)); #AThe example stores a PIN and validation method privately, then exposes unlocking through a public method. section 5.6.1













