WeakMap better than Private Symbols? (was: direct_proxies "problem")

Nathan Wall nathan.wall at live.com
Thu Jan 10 09:15:56 PST 2013

Brendan Eich wrote:
> Nathan Wall wrote:
> > In the case of (2), the implementation using a private symbol will fail on a call to `setTime` when the object is frozen (I think).
> Nope, we agreed that frozen objects support setting pre-existing (added
> before the object was made non-extensible) properties named by private
> symbols.

I wasn't aware, but that significantly improves the case for symbols. I'm glad to hear it!  However, it also begs the question of why symbols should behave differently from WeakMaps/closures in the presence of Proxies (via unknownPrivateSymbol trap).

David Bruant wrote:
> > In the case of (1), the implementation using a private symbol will have internal accesses to the `timestamp` symbol exposed through the unknownPrivateSymbol trap.
> The trap does not expose the symbol. That would be a bad leak.
> The third "whitelist" argument in the proxy constructor is here to
> communicate whether the proxy knows the privatesymbol or not.
> When [[Get]] or [[Set]] (or other property operations) happens with a
> whitelisted private symbol, the get or set (or other) trap is called
> with the symbol itself (since it's known). If the name isn't in the
> whitelist, the unknownPrivateSymbol trap is called with target as the
> *only* trap argument (the private name is not exposed and that's on
> purpose!)

I'm sorry, I was unclear. I understand that the symbol itself is not exposed, but the fact that some symbol was accessed is. It wold allow a proxy to throw to prevent access to internal state (right?). This is different from what would occur in the WeakMap implementation, and if I'm trying to hide internal state, then I don't want proxies to be able to mess with attempts to access it, so I'll choose to use WeakMaps anytime I want to write defensive code.

Of course, if I'm just writing something quick and dirty, I'll use Symbols because I would much prefer to use the simpler syntax Symbols provide.  But again, as a library author, allowing Proxies to interfere here seems like a weaker position for a generic object than using a WeakMap to hide this.


More information about the es-discuss mailing list