1

Edit: this isn't a duplicate o.O

I was the one who asked the "duplicate" question, but these are different animals though.

Here is what I want to finally achieve, which I explained already: https://jsfiddle.net/kpion/osqsz83g/

The code I showed is just a boiled down problem I have.

So no, unless I misunderstood, none of the comments are helpful :(

What I need is to get arguments passed to the 'get' trap, so I can then call the method in many other objects, passing the arguments.

Original:

I want to use the Proxy object to trap function call. And I need to know what arguments the user used when calling the function.

Like here:

(function() {
    'use strict';
    console.clear();

    //some empty class where I want to trap methods & props
    class X {

    }

    let proxy = {
        get: function(target, prop, receiver) {
            console.log(arguments);//this gives the arguments of the 'get' trap itself. 

            //I know I can do this to play with arguments, but this doesn't solve my another problem.
            /*
            return function(...args) {
                console.log('wrapper args:', args);
                return 42;
            }*/
        },


    };


    let p = new Proxy(X, proxy);
    console.log(p.test('some arg passed'));

})();

I do p.test('some arg passed') and I want to read the 'some arg passed' in the trap.

Working code: https://jsfiddle.net/kpion/44L8veLa/3/

If you think this might by XY problem, then yes, it's possible :)

The above is a boiled down code, my final goal is to make a simple wrapper around the querySelectorAll. I want to make this possible:

coolSelector('div').setAttribute('a','b');

With only -a few lines of code- class. Without messing with prototypes or anything like that. Just by calling the already existing DOM methods. Currently I have this:

https://jsfiddle.net/kpion/osqsz83g/

Edit2:

In case anybody's interested, here the struggle continues: Handling property.subproperty in Proxy handler

konrados
  • 1,047
  • 8
  • 21
  • You haven't explained what "my another problem" is. The commented-out code looks like the correct way to do this. – Domenic Apr 29 '18 at 20:50
  • 2
    `get` doesn't trap a call. It just traps the property access. To trap a call, you need the [apply trap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/apply), but it sounds like you don't want to trap a specific function. If you want to trap the call to the returned function, then you can put the "apply trap" on that. –  Apr 29 '18 at 20:50
  • Sounds like you don't need a proxy here at all, just implement the `test` function. – Bergi Apr 29 '18 at 20:53
  • 1
    ...is there a reason you don't just extend the `.prototype` of some object that is returned by `coolSelector`? Messing with Proxy sure seems a lot more complicated than messing with prototypes. –  Apr 29 '18 at 20:53
  • @Domenic - I updated my question, could you take a look again? This is what I really want to achieve - https://jsfiddle.net/kpion/osqsz83g/ and this is my 'another problem' – konrados Apr 29 '18 at 21:10
  • @CrazyTrain - I did play with 'apply' trap yesterday, no, it doesn't trap it. 'get' does :) – konrados Apr 29 '18 at 21:11
  • The apply trap goes on the function. There's no call when accessing the property. The get trap doesn't trap the call. –  Apr 29 '18 at 21:13
  • @CrazyTrain - thanks... but it does on Firefox according to this: https://jsfiddle.net/kpion/19o1mp0r/ - am I missing something here? The 'get' gets called, the 'apply' does not. – konrados Apr 29 '18 at 21:19
  • 1
    Right, your `get` trap is trapping the property access. There's no call at that point, but you then return a function, which does get invoked. The `apply` trap needs to be put directly on a function, not on a property access because the property access doesn't know if you're going to follow up with a function call or not. –  Apr 29 '18 at 21:22
  • 1
    ...actually, since you seem to want to "forward" the property or method to each member of a DOM collection, maybe you should just set up the `get` trap, do a quick check against the `Element.prototype[prop]` to see if it's a property method or not, and if a method, return your function that closes over the `prop` name and iterates the collection. If not a method, then it just returns an array of the value for each element's property. –  Apr 29 '18 at 21:27
  • @CrazyTrain - yeees! Finally someone understood me :) - it works - https://jsfiddle.net/kpion/osqsz83g/ - now I only hope I'll be able to handle property access (as in .style.somethiing = ) as well. Thank you very much again! – konrados Apr 29 '18 at 21:55
  • 1
    Yep, you're welcome. –  Apr 29 '18 at 22:00
  • 1
    @CrazyTrain as you seem to have fix the issue of this question, can you confirm or not if the question is answered in the duplicate ? Related to https://meta.stackoverflow.com/q/366840/7076153. – Stargateur Apr 29 '18 at 23:31
  • 1
    @Stargateur: The way this question was phrased, yeah, I think that's a duplicate. The question suggests that this may be an XY Problem, and it pretty much is. If instead of asking about a solution, the OP would have more directly and clearly explained the problem, it would be more distinct. –  Apr 30 '18 at 00:22
  • @konrados: There are too many possibilities to leave things open to interpretation, so we need to focus on the most unambiguous parts of the question. If you had up front stated that you want to treat a DOM HTMLCollection as though it's an Element by allowing all the same property and method access, and would like to do it using `Proxy` to not have to manually wrap everything, but didn't know how to differentiate between a method call and mere property access, it would've been much easier to understand the actual problem from the start. –  Apr 30 '18 at 00:28
  • ...but I hope you're ultimately satisfied having received some useful info. :-) –  Apr 30 '18 at 00:28
  • @CrazyTrain - yes, I'm happy with the info, thank you very much again! Since we are at this... here is a continuation - https://stackoverflow.com/questions/50092149/handling-property-subproperty-in-proxy-handler :) Here I tried really hard to avoid 'closing as duplicate', by showing the full code and full issue, without trying to make a "TL;DR" version :) I would be very grateful if you took a look at it :) – konrados Apr 30 '18 at 00:32
  • 1
    Yes, that looks much clearer to me, though I'm benefiting from some background now. I'll take a closer look at that one to see if I have any ideas. –  Apr 30 '18 at 00:35

0 Answers0