11

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()
11
  • No, but there might be nonstandard methods. See stackoverflow.com/a/38385693/283863 Commented Nov 10, 2017 at 5:28
  • Can you add that comment as an answer Commented Nov 10, 2017 at 5: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. Commented Nov 10, 2017 at 5: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. Commented Nov 10, 2017 at 5:34
  • You can’t. Also, don’t use proxies for anything, ever. Commented Nov 10, 2017 at 5:50

3 Answers 3

11

Just destructure it into a new object:

myNewSimpleObject = {...myProxy}

Cheers!

Sign up to request clarification or add additional context in comments.

1 Comment

what is the reasoning/explanation here? this won't give you a reference to the original object tmk
9

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

1 Comment

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.
5

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

3 Comments

This is genius and saved me a lot of time.
why/how does this work?
it just creates a new object with same values as the original

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.