Proxy's optional target

Dmitry Soshnikov dmitry.soshnikov at gmail.com
Mon Jan 14 21:53:25 PST 2013


On Jan 14, 2013, at 9:32 PM, Dmitry Soshnikov 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) {
>    ...
>  }
> });


Although... Actually, never-mind, I just looked at it again, and it looks a bit weird having some first argument "target" w/o actually specifying the one at creation. So yeah, explicit target makes sense. I just wanted to exclude these use-cases:

var p = new Proxy({}, handler);

where this use-less empty "{}" will be to many in proxies' code.

P.S.: I know it's not gonna happen, but in an "imaginary" world:

Anyways, if the explicit target, then it would be good refer it as `this` value instead of explicit parameter. But in this case we cannot get reference to the handler object (currently `this` refers to the handler object). But. Since proxies provide some internal meta-level API handling, we can refer the handler's method as private symbols instead. E.g.:

var p = new Proxy({
  @get: function (name, value) {
    if (this. at getOwnPropertyDescriptor(...)) {
      this[name] = value;
    }
  }
});

`this` refers to the target, but this. at getOwnPropertyDescriptor(...) to the function of the handler object. But this is only at implementation level, from the end-level user's perspective, it looks like `this` is the target and `this. at getOwnPropertyDescriptor(...)` is the internal method hook on exactly the same target.

In this case, optional target becomes completely implicit and can be treated as just a "traceable" object.

This doesn't reflect some invariants as `p === this`, if p is the proxy and `this` is the target (and this is why p.name = 10; triggers the handler and this.name = 10; inside the handler function does not), so, just thinking saying.

Dmitry


More information about the es-discuss mailing list