W3C home > Mailing lists > Public > public-script-coord@w3.org > January to March 2015

Re: Creating entangled objects

From: Mark S. Miller <erights@google.com>
Date: Tue, 10 Mar 2015 11:47:45 -0700
Message-ID: <CABHxS9iuQim+b0RzL2BstvRMZ_2SLxc5h02gjDW4aF47cAnHCw@mail.gmail.com>
To: Jonas Sicking <jonas@sicking.cc>
Cc: "Tab Atkins Jr." <jackalmage@gmail.com>, David Dorwin <ddorwin@google.com>, public-script-coord <public-script-coord@w3.org>, Boris Zbarsky <bzbarsky@mit.edu>
On Mon, Mar 9, 2015 at 6:44 PM, Jonas Sicking <jonas@sicking.cc> wrote:

> On Mon, Mar 9, 2015 at 5:32 PM, Mark S. Miller <erights@google.com> wrote:
> > Object.create(OtherObjectPrototype)
>
> That means that the object is still instantiateable on its own.
>
> Also, how do you entagle the object with the other object in a clean way?
>
> / Jonas
>
> > On Mon, Mar 9, 2015 at 4:57 PM, Jonas Sicking <jonas@sicking.cc> wrote:
> >>
> >> On Mon, Mar 9, 2015 at 3:15 PM, Mark S. Miller <erights@google.com>
> wrote:
> >> > On Mon, Mar 9, 2015 at 2:40 PM, Tab Atkins Jr. <jackalmage@gmail.com>
> >> > wrote:
> >> >>
> >> >> On Mon, Mar 9, 2015 at 2:37 PM, Jonas Sicking <jonas@sicking.cc>
> wrote:
> >> >> > On Mon, Mar 9, 2015 at 1:10 PM, Mark S. Miller <erights@google.com
> >
> >> >> > wrote:
> >> >> >> I can see why it may need a prototype. But why does it need a
> >> >> >> constructor?
> >> >> >
> >> >> > From what I'm told, in order to explain how the object was created.
> >> >> > I.e. to avoid building "magic" into the API.
> >> >> >
> >> >> > But maybe there are other ways to do that?
> >> >>
> >> >> This is a cool discussion, but it's also a complete tangent from the
> >> >> original thread. ^_^
> >> >
> >> >
> >> > Hi Tab, Good point. Changing title to start new thread.
> >> >
> >> > Hi Jonas, I don't understand. If the two objects are entangled, having
> >> > one
> >> > call that creates both seems like a better explanation than pretending
> >> > to
> >> > have two constructors. That the two objects have different APIs and
> >> > methods
> >> > are adequately explained by different prototypes.
> >>
> >> But how does the call that create the two objects create them? If not
> >> through their constructor?
>


Warning: Untested code follows.



var UpCounter = (function() {
'use strict';

const ups = new WeakMap();
const downs = new WeakMap();

DownCounterPrototype = Object.freeze({
    decr() { downs.get(this).counter--; }
});

return class {
  constructor(factory) {
    const down = Object.create(DownCounterPrototype);
    const hiddenState = { counter: 0 };
    downs.set(down, hiddenState);
    ups.set(this, hiddenState);
    factory(down);
  }
  incr() { ups.get(this).counter++; }
};

}());


-- 
    Cheers,
    --MarkM
Received on Tuesday, 10 March 2015 18:48:11 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 10 March 2015 18:48:13 UTC