Proxy's optional target

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon Jan 14 22:09:59 PST 2013


and `this` is the handler, not the target ...

var
  handler,
  target = {test:123},
  proxy = new Proxy(target, handler = {
    get: function ($target, key) {
      alert($target === target); // true
      alert(this === handler);   // true
      return $target[key];
    }
  })
;
alert(proxy.test); // 123

and no, I don;'t think everyone gonna proxy empty objects but if that's the
case you want that because either you are looking for a singleton,
otherwise what's the point to have same target per each new handler? Your
concern is the othr way round too, isn't it?

If you use new Proxy you want a different target, I guess, otherwise you
are most likely looking for same handler, different targets, which makes
more sense, imho

new Proxy({/*fresh new target*/}, sameHandler)





On Mon, Jan 14, 2013 at 9:55 PM, Dmitry Soshnikov <
dmitry.soshnikov at gmail.com> wrote:

>
> On Jan 14, 2013, at 9:51 PM, Andrea Giammarchi wrote:
>
> uhm, you might be slightly behind current specs ... these changed proxy
> quite a lot so that first argument is the target, and second argument is
> the behavior.
>
> var myProxy = new Proxy(target, handler);
>
>
> Oh, I'm aware about current pre-spec MDC article and API. And this is why
> saying. In most use-cases you probably wanna proxy an empty object, and
> this is why in current API you end up in code like this:
>
> var p = new Proxy({}, handler);
>
> And I'm saying about these "{}" always as the first argument.
>
> Dmitry
>
> at least that's how Firefox implemented it right now :D
>
> var
>   target = {test:123},
>   proxy = new Proxy(target, {
>     get: function ($target, key) {
>       alert($target === target); // true
>       return $target[key];
>     }
>   })
> ;
> alert(proxy.test); // 123
>
>
> On Mon, Jan 14, 2013 at 9:32 PM, Dmitry Soshnikov <
> dmitry.soshnikov at gmail.com> wrote:
>
>> Hello,
>>
>> Don't know whether it was mentioned/asked before (sorry if so), but just
>> a note: probably it makes more sense making the target argument as optional
>> and the second one in the Proxy constructor's API.
>>
>> Proxy(handler[, target]):
>>
>> 1. If target is undefined, let the target be new Object();
>> ...
>>
>> In this case we'll cover (probably the most used) use-case of
>> direct-proxies:
>>
>> var p = new Proxy({
>>   get: function(target, name, value) {
>>     ...
>>   }
>> });
>>
>> Thanks,
>> Dmitry
>> _______________________________________________
>> 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/20130114/e5001108/attachment.html>


More information about the es-discuss mailing list