-
Notifications
You must be signed in to change notification settings - Fork 26.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggest using WeakMaps when hidden properties are needed #1722
Conversation
This updates the "trailing or leading underscores" guideline to suggest an better way to make properties hidden, as an alternative to just removing the underscore and making the property public.
README.md
Outdated
@@ -2993,6 +2993,10 @@ Other Style Guides | |||
|
|||
// good | |||
this.firstName = 'Panda'; | |||
|
|||
// good |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this is useful, but we should include a note warning about browser support that links to http://kangax.github.io/compat-table/es6/#test-WeakMap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, let me know if that was what you had in mind.
May I use this topic as a learning experience? :-) Why WeakMap, over any other particular thing? |
@ericblade Using a
|
.... OK, it took me a minute of thinking this through, perhaps a more in-depth explanation of the situation, or slightly more in-depth code example would be helpful (or perhaps I missed it, just reading that specific updated section).. In this particular case, you have a
.. and the WeakMap is a Map, so that multiple instances of the class can use the same data store for all of them, using "this" as the key. That was the part that wasn't obviously clear to me -- at a quick glance, I failed to notice that we were talking about class instances, rather than like a module, in which case, basically any data structure would also work (so long as it wasn't containing references to external objects) |
This updates the "trailing or leading underscores" guideline to suggest an better way to make properties hidden, as an alternative to just removing the underscore and making the property public.
This is motivated by a discussion with @ljharb today in the
eslint/eslint
Gitter channel.