Catch-all proposal based on proxies

Tom Van Cutsem tomvc at google.com
Wed Dec 9 13:57:53 PST 2009


On Wed, Dec 9, 2009 at 1:02 AM, Mike Wilson <mikewse at hotmail.com> wrote:

> Looking at the Use Cases section in your proposal it seems most
> of your suggested use cases would be interesting to apply on an
> object prototype/class level when targeting many objects, to
> avoid having to recreate the catch-all for every object and also
> possibly adding dynamically generated methods in a common place
> (like in Ruby/ActiveRecord).
> Hearing that you are not certain that this is indeed an important
> use case, or maybe use case property, I wonder if I am missing
> something? I have full respect for that you have thought this
> through, so maybe there is something in the language or in other
> Harmony plans that make this less interesting?
>

Ok, we should probably have made clear the distinction between our
*motivating* use cases and other possible use cases.
Our motivating use case for the proxy proposal is near-complete object
virtualization, that is: the ability to emulate as much aspects of an
object's behavior as practically possible (I'm not claiming our current
proposal achieves full virtualization, but it comes close, given the legacy
compatibility and security constraints).

In this light, the "__doesNotUnderstand__" trap provides a strict subset of
what you can do with full object virtualization. So, while you can
definitely use the proposed proxies to build a Ruby/ActiveRecord-style
framework in Javascript (see below), this use case was not our main
motivation. A motivating example would be using proxies to virtualize an
existing Javascript API (e.g. to enforce access control to the DOM, or to
provide a legacy interface adaptor to a new API).


>  This sounds interesting. It would be great if you could show a
> code example? A good "demo" case could be mimicing the single
> point catch-all and dynamic .find_by_* method generation for all
> User objects in the ActiveRecord examples:
> http://api.rubyonrails.org/classes/ActiveRecord/Base.html
> (section "Dynamic attribute-based finders")
>

Here's the essence of one possible solution:

var attributeBasedFinder = Proxy.create({
  get: function(rcvr, p) {
    var res = p.match(/^find_by_(.*)$/);
    if (res) {
      return generate_find_by_query(res[1], rcvr.name);
    }
  }
});

function makeTable(tname) {
  return Object.create(attributeBasedFinder, {
    name: { value: tname }
  });
};

Cheers,
Tom


>
> Best regards
> Mike
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20091209/3b64e45f/attachment.html>


More information about the es-discuss mailing list