Last active
October 22, 2023 15:17
-
-
Save conartist6/95f5be3488b2ae6c0dfd9a8867fe2330 to your computer and use it in GitHub Desktop.
Private/readonly class properties with weak maps
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
// 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