How to get the target of a JavaScript Proxy?









up vote
0
down vote

favorite












function createProxy() 
const myArray = [Math.random(), Math.random()];
return new Proxy(myArray, );


const myProxy = createProxy();


How to access the target (which is myArray) of myProxy here?



I've tried many ways. Was googled many blog posts, but found no way to get the target :(










share|improve this question

















  • 1




    You can't. That's why it's a proxy. What are you trying to do, why do you need this?
    – Bergi
    Jun 29 at 7:54










  • @Bergi Because if I have an object with circular references, and these circular references are in proxies, there is no way to safe stringify my object. I'll get stack size exceeded error. :(
    – Adam
    Jun 29 at 7:59







  • 1




    Can you make an example of that, please? Circular references (I think you mean those, not dependencies) should work just fine with proxies, as myProxy === myProxy still holds. Nothing needs to get its hands on the target.
    – Bergi
    Jun 29 at 8:02














up vote
0
down vote

favorite












function createProxy() 
const myArray = [Math.random(), Math.random()];
return new Proxy(myArray, );


const myProxy = createProxy();


How to access the target (which is myArray) of myProxy here?



I've tried many ways. Was googled many blog posts, but found no way to get the target :(










share|improve this question

















  • 1




    You can't. That's why it's a proxy. What are you trying to do, why do you need this?
    – Bergi
    Jun 29 at 7:54










  • @Bergi Because if I have an object with circular references, and these circular references are in proxies, there is no way to safe stringify my object. I'll get stack size exceeded error. :(
    – Adam
    Jun 29 at 7:59







  • 1




    Can you make an example of that, please? Circular references (I think you mean those, not dependencies) should work just fine with proxies, as myProxy === myProxy still holds. Nothing needs to get its hands on the target.
    – Bergi
    Jun 29 at 8:02












up vote
0
down vote

favorite









up vote
0
down vote

favorite











function createProxy() 
const myArray = [Math.random(), Math.random()];
return new Proxy(myArray, );


const myProxy = createProxy();


How to access the target (which is myArray) of myProxy here?



I've tried many ways. Was googled many blog posts, but found no way to get the target :(










share|improve this question













function createProxy() 
const myArray = [Math.random(), Math.random()];
return new Proxy(myArray, );


const myProxy = createProxy();


How to access the target (which is myArray) of myProxy here?



I've tried many ways. Was googled many blog posts, but found no way to get the target :(







javascript ecmascript-6 proxy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 29 at 7:33









Adam

2,188827




2,188827







  • 1




    You can't. That's why it's a proxy. What are you trying to do, why do you need this?
    – Bergi
    Jun 29 at 7:54










  • @Bergi Because if I have an object with circular references, and these circular references are in proxies, there is no way to safe stringify my object. I'll get stack size exceeded error. :(
    – Adam
    Jun 29 at 7:59







  • 1




    Can you make an example of that, please? Circular references (I think you mean those, not dependencies) should work just fine with proxies, as myProxy === myProxy still holds. Nothing needs to get its hands on the target.
    – Bergi
    Jun 29 at 8:02












  • 1




    You can't. That's why it's a proxy. What are you trying to do, why do you need this?
    – Bergi
    Jun 29 at 7:54










  • @Bergi Because if I have an object with circular references, and these circular references are in proxies, there is no way to safe stringify my object. I'll get stack size exceeded error. :(
    – Adam
    Jun 29 at 7:59







  • 1




    Can you make an example of that, please? Circular references (I think you mean those, not dependencies) should work just fine with proxies, as myProxy === myProxy still holds. Nothing needs to get its hands on the target.
    – Bergi
    Jun 29 at 8:02







1




1




You can't. That's why it's a proxy. What are you trying to do, why do you need this?
– Bergi
Jun 29 at 7:54




You can't. That's why it's a proxy. What are you trying to do, why do you need this?
– Bergi
Jun 29 at 7:54












@Bergi Because if I have an object with circular references, and these circular references are in proxies, there is no way to safe stringify my object. I'll get stack size exceeded error. :(
– Adam
Jun 29 at 7:59





@Bergi Because if I have an object with circular references, and these circular references are in proxies, there is no way to safe stringify my object. I'll get stack size exceeded error. :(
– Adam
Jun 29 at 7:59





1




1




Can you make an example of that, please? Circular references (I think you mean those, not dependencies) should work just fine with proxies, as myProxy === myProxy still holds. Nothing needs to get its hands on the target.
– Bergi
Jun 29 at 8:02




Can you make an example of that, please? Circular references (I think you mean those, not dependencies) should work just fine with proxies, as myProxy === myProxy still holds. Nothing needs to get its hands on the target.
– Bergi
Jun 29 at 8:02












5 Answers
5






active

oldest

votes

















up vote
1
down vote













There is a clever way to do this - You can add a get trap to the proxy and have it return the target conditionally. Like so..



let resolveMode = false; // Switch that controls if getter returns target or prop. 

function resolve(obj)
resolveMode = true; // Turn on our switch
let target = obj.anything; // This gets the target not the prop!
resolveMode = false; // Turn off the switch for the getter to behave normally
return target; // Return what we got!


function createProxy()
const myArray = [Math.random(), Math.random()];
return new Proxy(myArray,
get: function(target, prop)
if (resolveMode) return target; // This is where the magic happens!
else return target[prop]; // This is normal behavior..

);


const myProxy = createProxy();
let target = resolve(myProxy);


Remember that the more lines of code you add to traps, the slower the object's performance gets. Hope this helps.






share|improve this answer



























    up vote
    0
    down vote













    You can make a copy of the data returned by the proxy using Object.assign():



    const target_copy = Object.assign(, my_proxy);


    This will work for all enumerable own properties existing on the proxy/target.






    share|improve this answer





























      up vote
      0
      down vote













      As the proxy contain an object you can also do



      Object.keys( my_proxy )


      And then it become easy to retrieve thing such as Object.keys( my_proxy ).length






      share|improve this answer



























        up vote
        0
        down vote













        You can if the target is an object.



        You will have to create a function in target to retrieve it, that's all.



        Example:



        class AnyClass 
        constructor()
        this.target = this;

        return new Proxy(this, this);


        get(obj, prop)
        if (prop in obj)
        return this[prop];

        // your stuff here


        getTarget()
        return this.target;




        And then when you call:



        let sample = new AnyClass;
        console.log(sample.getTarget());


        Will return you the target as you expect :)






        share|improve this answer



























          up vote
          0
          down vote













          How about adding the following get trap:



          const handler = 
          get: (target, property, receiver) =>
          if (property === 'myTarget')
          return target

          return target[property]



          const myArray = [Math.random(), Math.random()];

          function createProxy()
          // const myArray = [Math.random(), Math.random()];
          return new Proxy(myArray, handler);


          const myProxy = createProxy();


          And you can get the target of the proxy by myProxy.myTarget:



          console.log(myProxy.myTarget) // [0.22089416118932403, 0.08429264462405173]
          console.log(myArray === myProxy.myTarget) // true





          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51096547%2fhow-to-get-the-target-of-a-javascript-proxy%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            There is a clever way to do this - You can add a get trap to the proxy and have it return the target conditionally. Like so..



            let resolveMode = false; // Switch that controls if getter returns target or prop. 

            function resolve(obj)
            resolveMode = true; // Turn on our switch
            let target = obj.anything; // This gets the target not the prop!
            resolveMode = false; // Turn off the switch for the getter to behave normally
            return target; // Return what we got!


            function createProxy()
            const myArray = [Math.random(), Math.random()];
            return new Proxy(myArray,
            get: function(target, prop)
            if (resolveMode) return target; // This is where the magic happens!
            else return target[prop]; // This is normal behavior..

            );


            const myProxy = createProxy();
            let target = resolve(myProxy);


            Remember that the more lines of code you add to traps, the slower the object's performance gets. Hope this helps.






            share|improve this answer
























              up vote
              1
              down vote













              There is a clever way to do this - You can add a get trap to the proxy and have it return the target conditionally. Like so..



              let resolveMode = false; // Switch that controls if getter returns target or prop. 

              function resolve(obj)
              resolveMode = true; // Turn on our switch
              let target = obj.anything; // This gets the target not the prop!
              resolveMode = false; // Turn off the switch for the getter to behave normally
              return target; // Return what we got!


              function createProxy()
              const myArray = [Math.random(), Math.random()];
              return new Proxy(myArray,
              get: function(target, prop)
              if (resolveMode) return target; // This is where the magic happens!
              else return target[prop]; // This is normal behavior..

              );


              const myProxy = createProxy();
              let target = resolve(myProxy);


              Remember that the more lines of code you add to traps, the slower the object's performance gets. Hope this helps.






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                There is a clever way to do this - You can add a get trap to the proxy and have it return the target conditionally. Like so..



                let resolveMode = false; // Switch that controls if getter returns target or prop. 

                function resolve(obj)
                resolveMode = true; // Turn on our switch
                let target = obj.anything; // This gets the target not the prop!
                resolveMode = false; // Turn off the switch for the getter to behave normally
                return target; // Return what we got!


                function createProxy()
                const myArray = [Math.random(), Math.random()];
                return new Proxy(myArray,
                get: function(target, prop)
                if (resolveMode) return target; // This is where the magic happens!
                else return target[prop]; // This is normal behavior..

                );


                const myProxy = createProxy();
                let target = resolve(myProxy);


                Remember that the more lines of code you add to traps, the slower the object's performance gets. Hope this helps.






                share|improve this answer












                There is a clever way to do this - You can add a get trap to the proxy and have it return the target conditionally. Like so..



                let resolveMode = false; // Switch that controls if getter returns target or prop. 

                function resolve(obj)
                resolveMode = true; // Turn on our switch
                let target = obj.anything; // This gets the target not the prop!
                resolveMode = false; // Turn off the switch for the getter to behave normally
                return target; // Return what we got!


                function createProxy()
                const myArray = [Math.random(), Math.random()];
                return new Proxy(myArray,
                get: function(target, prop)
                if (resolveMode) return target; // This is where the magic happens!
                else return target[prop]; // This is normal behavior..

                );


                const myProxy = createProxy();
                let target = resolve(myProxy);


                Remember that the more lines of code you add to traps, the slower the object's performance gets. Hope this helps.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 17:06









                Soumik Chatterjee

                111




                111






















                    up vote
                    0
                    down vote













                    You can make a copy of the data returned by the proxy using Object.assign():



                    const target_copy = Object.assign(, my_proxy);


                    This will work for all enumerable own properties existing on the proxy/target.






                    share|improve this answer


























                      up vote
                      0
                      down vote













                      You can make a copy of the data returned by the proxy using Object.assign():



                      const target_copy = Object.assign(, my_proxy);


                      This will work for all enumerable own properties existing on the proxy/target.






                      share|improve this answer
























                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote









                        You can make a copy of the data returned by the proxy using Object.assign():



                        const target_copy = Object.assign(, my_proxy);


                        This will work for all enumerable own properties existing on the proxy/target.






                        share|improve this answer














                        You can make a copy of the data returned by the proxy using Object.assign():



                        const target_copy = Object.assign(, my_proxy);


                        This will work for all enumerable own properties existing on the proxy/target.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Sep 25 at 6:05

























                        answered Sep 25 at 5:15









                        Rashad Saleh

                        944713




                        944713




















                            up vote
                            0
                            down vote













                            As the proxy contain an object you can also do



                            Object.keys( my_proxy )


                            And then it become easy to retrieve thing such as Object.keys( my_proxy ).length






                            share|improve this answer
























                              up vote
                              0
                              down vote













                              As the proxy contain an object you can also do



                              Object.keys( my_proxy )


                              And then it become easy to retrieve thing such as Object.keys( my_proxy ).length






                              share|improve this answer






















                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                As the proxy contain an object you can also do



                                Object.keys( my_proxy )


                                And then it become easy to retrieve thing such as Object.keys( my_proxy ).length






                                share|improve this answer












                                As the proxy contain an object you can also do



                                Object.keys( my_proxy )


                                And then it become easy to retrieve thing such as Object.keys( my_proxy ).length







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Oct 4 at 10:56









                                Raccoon

                                6511125




                                6511125




















                                    up vote
                                    0
                                    down vote













                                    You can if the target is an object.



                                    You will have to create a function in target to retrieve it, that's all.



                                    Example:



                                    class AnyClass 
                                    constructor()
                                    this.target = this;

                                    return new Proxy(this, this);


                                    get(obj, prop)
                                    if (prop in obj)
                                    return this[prop];

                                    // your stuff here


                                    getTarget()
                                    return this.target;




                                    And then when you call:



                                    let sample = new AnyClass;
                                    console.log(sample.getTarget());


                                    Will return you the target as you expect :)






                                    share|improve this answer
























                                      up vote
                                      0
                                      down vote













                                      You can if the target is an object.



                                      You will have to create a function in target to retrieve it, that's all.



                                      Example:



                                      class AnyClass 
                                      constructor()
                                      this.target = this;

                                      return new Proxy(this, this);


                                      get(obj, prop)
                                      if (prop in obj)
                                      return this[prop];

                                      // your stuff here


                                      getTarget()
                                      return this.target;




                                      And then when you call:



                                      let sample = new AnyClass;
                                      console.log(sample.getTarget());


                                      Will return you the target as you expect :)






                                      share|improve this answer






















                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        You can if the target is an object.



                                        You will have to create a function in target to retrieve it, that's all.



                                        Example:



                                        class AnyClass 
                                        constructor()
                                        this.target = this;

                                        return new Proxy(this, this);


                                        get(obj, prop)
                                        if (prop in obj)
                                        return this[prop];

                                        // your stuff here


                                        getTarget()
                                        return this.target;




                                        And then when you call:



                                        let sample = new AnyClass;
                                        console.log(sample.getTarget());


                                        Will return you the target as you expect :)






                                        share|improve this answer












                                        You can if the target is an object.



                                        You will have to create a function in target to retrieve it, that's all.



                                        Example:



                                        class AnyClass 
                                        constructor()
                                        this.target = this;

                                        return new Proxy(this, this);


                                        get(obj, prop)
                                        if (prop in obj)
                                        return this[prop];

                                        // your stuff here


                                        getTarget()
                                        return this.target;




                                        And then when you call:



                                        let sample = new AnyClass;
                                        console.log(sample.getTarget());


                                        Will return you the target as you expect :)







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 7 at 18:20









                                        ragnar

                                        958915




                                        958915




















                                            up vote
                                            0
                                            down vote













                                            How about adding the following get trap:



                                            const handler = 
                                            get: (target, property, receiver) =>
                                            if (property === 'myTarget')
                                            return target

                                            return target[property]



                                            const myArray = [Math.random(), Math.random()];

                                            function createProxy()
                                            // const myArray = [Math.random(), Math.random()];
                                            return new Proxy(myArray, handler);


                                            const myProxy = createProxy();


                                            And you can get the target of the proxy by myProxy.myTarget:



                                            console.log(myProxy.myTarget) // [0.22089416118932403, 0.08429264462405173]
                                            console.log(myArray === myProxy.myTarget) // true





                                            share|improve this answer


























                                              up vote
                                              0
                                              down vote













                                              How about adding the following get trap:



                                              const handler = 
                                              get: (target, property, receiver) =>
                                              if (property === 'myTarget')
                                              return target

                                              return target[property]



                                              const myArray = [Math.random(), Math.random()];

                                              function createProxy()
                                              // const myArray = [Math.random(), Math.random()];
                                              return new Proxy(myArray, handler);


                                              const myProxy = createProxy();


                                              And you can get the target of the proxy by myProxy.myTarget:



                                              console.log(myProxy.myTarget) // [0.22089416118932403, 0.08429264462405173]
                                              console.log(myArray === myProxy.myTarget) // true





                                              share|improve this answer
























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                How about adding the following get trap:



                                                const handler = 
                                                get: (target, property, receiver) =>
                                                if (property === 'myTarget')
                                                return target

                                                return target[property]



                                                const myArray = [Math.random(), Math.random()];

                                                function createProxy()
                                                // const myArray = [Math.random(), Math.random()];
                                                return new Proxy(myArray, handler);


                                                const myProxy = createProxy();


                                                And you can get the target of the proxy by myProxy.myTarget:



                                                console.log(myProxy.myTarget) // [0.22089416118932403, 0.08429264462405173]
                                                console.log(myArray === myProxy.myTarget) // true





                                                share|improve this answer














                                                How about adding the following get trap:



                                                const handler = 
                                                get: (target, property, receiver) =>
                                                if (property === 'myTarget')
                                                return target

                                                return target[property]



                                                const myArray = [Math.random(), Math.random()];

                                                function createProxy()
                                                // const myArray = [Math.random(), Math.random()];
                                                return new Proxy(myArray, handler);


                                                const myProxy = createProxy();


                                                And you can get the target of the proxy by myProxy.myTarget:



                                                console.log(myProxy.myTarget) // [0.22089416118932403, 0.08429264462405173]
                                                console.log(myArray === myProxy.myTarget) // true






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 22 at 13:25

























                                                answered Nov 22 at 13:20









                                                Yuci

                                                3,7012734




                                                3,7012734



























                                                    draft saved

                                                    draft discarded
















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid


                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.

                                                    To learn more, see our tips on writing great answers.





                                                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                    Please pay close attention to the following guidance:


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid


                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.

                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51096547%2fhow-to-get-the-target-of-a-javascript-proxy%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    這個網誌中的熱門文章

                                                    How to read a connectionString WITH PROVIDER in .NET Core?

                                                    Node.js Script on GitHub Pages or Amazon S3

                                                    Museum of Modern and Contemporary Art of Trento and Rovereto