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