Are Private name and Weak Map the same feature? and the Assoc API

Andreas Rossberg rossberg at google.com
Tue Dec 20 05:45:42 PST 2011


On 17 December 2011 00:24, Sam Tobin-Hochstadt <samth at ccs.neu.edu> wrote:
>
> I used to think this way about private names and proxies, and argued
> for this position. However, I was wrong and it's a bad idea.  Here's
> why:
>
> You might want to implement an object with a private field and a
> public method like this:
>
> let o = (function () { let n = new Name(); return { [n]: 0; get:
> function() { return this[n]; }}})();
>
> But, if we don't censor private names in proxies, this abstraction leaks!
>
> Consider:
>
> let p = new Proxy(...);
> o.get.call(p);
>
> Now the name is leaked to the proxy.  And there's no way you can fix
> this, without resorting to some other method of hiding things like
> closures.

Hm, isn't this example rather demonstrating that the ability to do
self stealing -- i.e., the lack of lexical `this' -- is violating
basic abstraction principles (as we all know)? That is, in JS you have
to manually fix such abstractions using .bind on your methods (or
potential nicer alternatives in ES6). Arguably, the problem is not
proxies or private names (under David's semantics) per se.

But of course, we cannot really fix `this', so we introduce public
name conversion (and other ad-hoc complications surrounding private
names) as compatibility hacks that prevent blatant leaks for existing
JS idioms. It's not puristic, but it is convenient and pragmatically
safer.

/Andreas


More information about the es-discuss mailing list