Reinforce data safety in ES modules and classes
guard function
top of the mdule/class file
const SYM_KEEPER = Symbol ('safety-guard-do-not-export')
define inside yout class:
#guard () {
if (SYM_KEPEER === sym_keeper) {
return true
} else {
throw new Error ('#SAFETY_GUARD!')
}
}
use in private methods
constructro () {
// ....
this.#setup (SYM_KEEPER)
}
#setup (sym_keeper) {
this.#guard(sym_keeper)
// ...
}
Top comments (0)