Kevin Smith wrote:
Thanks Dave,

Of the 3 use cases you mentioned, I think unique names are probably sufficient for 1 and 3. For the second use case (an inaccessible piece of data associated with an object), would not a weak map also be appropriate?

No, WeakMaps have two problems we've covered in this list:

1. Less efficient than private names.

This matters when you can least afford it, and it matters for private names used to program in the large using objects in JS. WeakMaps require special GC handling and they're an extra object with internal mutable state. Private name objects are flat, frozen, and can be optimized a lot harder.

2. You cannot abstract property access:

  function get(obj, prop) { return obj[prop]; }

works with a private name object referenced by prop. No such abstraction can be done with a weak map.

/be
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to