9

Say I have a proxy instance like so:

const getProxy = function(){
    return new Proxy({}, ...);
}

const proxy = getProxy();

later on, I want to retrieve the target from the proxy, is there some way to do this? something like:

const target = proxy.getOriginalTarget()
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • No, but there might be nonstandard methods. See https://stackoverflow.com/a/38385693/283863 – Derek 朕會功夫 Nov 10 '17 at 05:28
  • Can you add that comment as an answer – Alexander Mills Nov 10 '17 at 05:29
  • 1
    Frankly I think this question is very similar to the one I mentioned, although that one is about getting the handler instead of the original target. I can write that as answer but I think the answer on that page is already sufficient. – Derek 朕會功夫 Nov 10 '17 at 05:33
  • This is more about the exact API call if there is one for this, since there appear to be no documents for this specific call that I can find. – Alexander Mills Nov 10 '17 at 05:34
  • You can’t. Also, don’t use proxies for anything, ever. – Ry- Nov 10 '17 at 05:50
  • You no like Proxies, for what reasons? – Alexander Mills Nov 10 '17 at 05:54
  • Why do you need a proxy? (You don’t – that’s the reason – but feel free to answer) – Ry- Nov 10 '17 at 06:04
  • this isn't the current use case - but this is one use case - https://github.com/sumanjs/suman/blob/master/lib/test-suite-helpers/suman-methods.ts#L98 – Alexander Mills Nov 10 '17 at 06:12
  • in the current use case, I want a object that throws if you assign any properties to it, but I don't want to freeze the original object, because that would effect outside scope where the object is referenced. So I surround the original object with a proxy, and I need the original object's prototype chain too. – Alexander Mills Nov 10 '17 at 06:13
  • here is an explanation - https://github.com/sumanjs/suman/blob/master/lib/test-suite-helpers/suman-methods.ts#L91 – Alexander Mills Nov 10 '17 at 06:14
  • One alternative, is to just use `x = Object.create(target)`, instead of `new Proxy(target)`...then if someone assigns something to x, it won't hit the target (because it's the `__proto__`), so it should prevent writes TMK. – Alexander Mills Nov 10 '17 at 06:17

3 Answers3

11

Just destructure it into a new object:

myNewSimpleObject = {...myProxy}

Cheers!

Adrian Nuske
  • 111
  • 1
  • 5
6

I haven't heard of standard solution, but you can build your factory function which returns new Proxy object with custom property:

function buildProxy(target, handler) {
    const proxy = new Proxy(target, handler);

    proxy.originalTarget = target;

    return proxy;
}

const test = {};
const handler = {};

buildProxy(test, handler).originalTarget === test; // true
Terbiy
  • 576
  • 5
  • 14
  • 1
    Many thanks - this is also the solution for keeping a reference to `target` for the purpose of the underlying object being used in a WeakMap (within the proxy handler `target` is the original, but anywhere in a class `this` references the proxy). This fixes that issue. – Shaun Oct 09 '20 at 06:34
6

One hack I've used to get around this in some circumstances is to use JSON.stringify to strip the Proxy object and then JSON.parse to return it as an object:

const test = {name: 'Proxy', test: true};
const handler = {};

const proxy = new Proxy(test, handler); // Proxy {name: "Proxy", test: true}

originalTarget = JSON.parse(JSON.stringify(proxy)); // {name: 'Proxy', test: true}

One important thing to note with this hack is that the properties of the new object can be equated to the properties of the original, but the whole object cannot, e.g:

originalTarget === test // false
originalTarget == test // false

originalTarget.name === test.name // true
originalTarget.test === test.test // true
Luke Scales
  • 93
  • 1
  • 5