Ducks, Rabbits, and Privacy

Kevin Smith khs4473 at
Wed Jan 23 12:00:47 PST 2013

> Hi Kevin, thanks for pulling this code example out of the gist and posting
> separately. Looking at it only in context before, for some reason I hadn't
> realized how beautiful this is. To support this pattern, your makePrivate()
> could be defined either in terms of either private symbols or weakmaps,
> right?

Sure - but note that the implementation in the gist doesn't use either, per
se.  It uses a new base-level API which provides the ability to get or set
private data for any (object, symbol) pair.  Advantages of this approach

- No need for for special case "private" object slots.
- Only one kind of Symbol.
- We can leave WeakMap as a strictly ephemeron data structure, like Allen
wants.  No GC hint required.

I find this to be an intriguing "middle way".  Here is the implementation
in full:

    function makePrivate() {

        // Create a symbol that names the private data
        var symbol = new Symbol();

        // Create a function for accessing the private data on some object
        var priv = (obj, ...args) => {

            if (args.length === 0) return Object.getPrivate(obj, symbol);
            else return Object.setPrivate(obj, symbol, args[0]);

        // Return the symbol so that we can share it...
        priv.symbol = symbol;

        return priv;

{ Kevin }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list