Suggest using WeakMaps when hidden properties are needed#1722
Suggest using WeakMaps when hidden properties are needed#1722ljharb merged 1 commit intoairbnb:masterfrom
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
| // good | ||
| this.firstName = 'Panda'; | ||
|
|
||
| // good |
There was a problem hiding this comment.
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.
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/eslintGitter channel.