Skip to content
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

Merged
merged 1 commit into from
Feb 7, 2018

Conversation

not-an-aardvark
Copy link
Contributor

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.

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
Copy link
Collaborator

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

Copy link
Contributor Author

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.

@ericblade
Copy link

May I use this topic as a learning experience? :-)

Why WeakMap, over any other particular thing?

@not-an-aardvark
Copy link
Contributor Author

@ericblade Using a WeakMap is useful for two reasons:

  • If you don't pass the WeakMap instance around to consumers of your object, then they can't access the value in the WeakMap associated with your object. This means that you can effectively store private properties on the object which are only accessible to your code.
  • If the consumers of your object stop it using the object and it gets garbage-collected, the private properties will also be garbage-collected.

@ericblade
Copy link

ericblade commented Feb 6, 2018

.... 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

const privateData = new WeakMap();

class thingWithPrivateData {
....
}

.. 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)
(like I said, that may be obvious with reading more context into it than just the individually changed area)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants