On Mon, Oct 1, 2012 at 9:56 PM, Brendan Eich <bren...@mozilla.com> wrote:

> But if we have a solid branding mechanism (like Domado's ideal in latest
> browsers? ;-) then that should be used universally and this becomes a
> don't-care.
>

MarkM suggested I should expound on what Domado does.

Domado uses an abstraction which I called 'Confidence', which I invented in
order to provide "class-like" behavior in terms of ES5; it is designed to
provide the security properties we needed with a minimum of implementation
mechanism, and is therefore not purely a branding abstraction. It uses one
WeakMap keyed by the instances (the objects "confided in"); the value is a
plain {} object which stores all of the “private fields” of the key-object.
There are four operations provided by a Confidence:

1. confide: add an instance to the WeakMap and create its private-state
record.

      function TameNode() {
        TameNodeConf.confide(this);
      }

2. guard: test that an instance is in the WeakMap and return it or throw.

      var TameNodeT = TameNodeConf.guard;
      ...
      TameBackedNode.prototype.appendChild = nodeMethod(function (child) {
        child = TameNodeT.coerce(child);
        ...
      });

3. p: given an instance, return its private-state record.

      var np = TameNodeConf.p.bind(TameNodeConf);
      ...
      TameBackedNode.prototype.removeChild = nodeMethod(function(child) {
        ...
        np(this).feral.removeChild(np(child).feral);
        ...
      });

4. protectMethod: given a function, return a function with a guarded this.

      var nodeMethod = TameNodeConf.protectMethod;
      (usage examples above)


Note that unlike closure-based encapsulation, Confidence provides _sibling
amplification_; that is, a node method on one object can access the private
properties of another object, not only its own. This is not ideal as a
default for writing robust programs, but is useful to Domado since its
siblings interact (e.g. appendChild operates on two nodes from the same
DOM). An alternative abstraction which deemphasized sibling amplification
would be, for example, if "protectMethod" were defined such that the
wrapped function received an extra private-state argument and there was no
separate "p" operation (though sibling amplification can still be achieved
by having a protected method not exposed on the prototype).

The WeakMap used is the browser's WeakMap if available; otherwise we use an
emulation layer with inferior but sufficient garbage-collection properties
(implemented by SES or ES5/3; Domado is unaware of the distinction).
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to