Skip to content

Instantly share code, notes, and snippets.

@conartist6
Last active October 22, 2023 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conartist6/95f5be3488b2ae6c0dfd9a8867fe2330 to your computer and use it in GitHub Desktop.
Save conartist6/95f5be3488b2ae6c0dfd9a8867fe2330 to your computer and use it in GitHub Desktop.
Private/readonly class properties with weak maps
// To keep the data truly private, just don't export these
const privateMaps = {
secret: new WeakMap(),
constant: new WeakMap(),
}
export class Test {
constructor(secret, constant) {
privateMaps.secret.set(this, secret);
privateMaps.constant.set(this, constant);
}
get constant() {
return privateMaps.constant.get(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment