17

For example, if I have this handler/proxy (from the MDN example)...

var handler = {
    get: function(target, name){
        return name in target?
            target[name] :
            37;
    }
};

var p = new Proxy({}, handler);
p.a = 1;
p.b = undefined;

console.log(p.a, p.b); // 1, undefined
console.log('c' in p, p.c); // false, 37

is it possible to probe the proxy, p, in some way that allows me to get the handler object back.

Something along the lines of:

p.__handler__   // returns handler object -> Object {get: handler.get(), set: handler.set(), ...}
p.__handler__.get  // returns get prop/fn of handler -> function(target, name){ ...}

Obviously, the various traps set up in the handler are still "known" to the proxy, but is there a clear-cut way to return them/ the handler from the proxy itself? If so, how?

I have no specific use-case for this at the moment, but I could see this being useful if you wanted to dynamically change a handler/traps after you already have a proxy.

5
  • 1
    You have to ask kindly. If the proxy does define a __handler__ property, it will respond to your request.
    – Bergi
    Jul 14, 2016 at 23:21
  • 2
    It's quite the point of proxies that consumers are not allowed to inspect it like that, they should treat it like any other object. If you are the creator of the proxy, it's rather trivial to keep a reference to the handler
    – Bergi
    Jul 14, 2016 at 23:24
  • @Bergi, it's strange to me that the "point of proxies" would be mandated by the standard in that way. I think ideally it would responsibility of the proxy's creator to declare the proxy public/exposed or private, even if 95% of the time the "right" answer is that consumers can't inspect/augment it. Jul 15, 2016 at 15:53
  • 3
    Yes, exactly, the proxy creator can expose the handler - just do new Proxy({__handler__: handler}, handler) if you need it. It's not exposed by default because 95% of the cases it's not wanted.
    – Bergi
    Jul 16, 2016 at 10:22
  • @Bergi, ah, I see what you mean now. It wasn't obvious to me from your original comment. Please consider adding/expanding on your thoughts as an answer. I think what you are saying/implying about proxies being hidden on purpose is insightful while your workaround solution for making it possible is very relevant and likely very applicable for anyone wondering this as well. Jul 16, 2016 at 18:13

3 Answers 3

10

ECMAScript provides no way to access the internal [[ProxyHandler]] nor [[ProxyTarget]] slots.

Some implementations may provide some non-standard ways, but don't take it for granted.

For example, on Firefox privileged code, you can know if an object is a proxy using

Components.utils.isProxy(object);

I proposed implementing similar methods to expose the [[ProxyHandler]] and [[ProxyTarget]]. They told me to implement them in Debugger.Object instead of Components.utils.

When the patch lands, it will be possible to use something like

Components.utils.import('resource://gre/modules/jsdebugger.jsm');
var Cc = Components.classes;

// Add a debugger to a new global
var global = new Components.utils.Sandbox(
  Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal),
  { freshZone: true }
);
addDebuggerToGlobal(global);
var dbg = new global.Debugger().addDebuggee(this);

// Create a debugger object of your object, and run proxy getters
var dbgObj = dbg.makeDebuggeeValue(object);
if(dbgObj.isProxy) { // a boolean
  dbgObj.proxyHandler.unsafeDereference(); // the [[ProxyHandler]]
  dbgObj.proxyTarget.unsafeDereference(); // the [[ProxyTarget]]
}
1
  • And what about CHrome?
    – Pacerier
    Sep 12, 2017 at 18:44
6

Add a "special" self descriptor property to getOwnPropertyDescriptor

const target = {
  //Fns ..
  //Props ...
};

const handler = {
  getOwnPropertyDescriptor(target, prop) {
    if(prop == "[[handler]]"){
        return { configurable: true, enumerable: true, value: this };
    }
    return undefined;
  },
  prop1: 'abcd'
  
};

const proxy = new Proxy(target, handler);

console.log(Object.getOwnPropertyDescriptor(proxy, '[[handler]]').value.prop1);

0

I could see this being useful if you wanted to dynamically change a handler/traps after you already have a proxy

If you just want to add handlers over the (proxied) object you already have access to: you could achieve this by creating a new Proxy that handles the specific traps you want to change, eg:

let newProxyWithDifferentGet = new Proxy(originalProxy, {
  get: (target, key){ ... }
}

If you wanted to access the original Proxy's target:

If you are the original Proxy's author, you can just do something like this when you construct it:

let openedProxy = new Proxy(Object.assign(target, {
  _originalHandler: handler,
  _originalTarget: target
}), handler)

If you're not the author, then whether or not that original target should be available to users is the decision of whoever wrote that original Proxy. If you disagree with that author about their encapsulation, that's a social problem, not a technical one, and this is not specific or unique to ES6's Proxies. If you're consuming open-source code, send a PR upstream explaining why you think the original target should be available to users, or just fork their code with your changes and use that, merging their updates to the original repo as you go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.