In the below code, How do I get access to the name of the function attempted to be called, and any params, in the catching function, in this example how can p.foo() (which is handled by the notFound function) print out the 'one','two','three' params as well. the amount of parameters should not be hardcoded as in this example 3.
I have tried replacing: return target['notFound']; with a bunch of combinations of () and [] and passing 'arguments' and even 'prop', no luck. also tried using .apply() still didnt get it to work.
var Foo = function () {
this.bar = function () {
return "found!";
}
this.notFound = function () {
return "method not found.";
}
};
var p = new Proxy(new Foo, {
get: function (target, prop) {
if (Object.keys(target).indexOf(prop) !== -1) {
return target[prop];
} else {
return target['notFound'];
}
}
});
console.log(p.bar()); // prints: found!;
console.log(p.foo("one","two","three")); // prints: method not found;
thanks in advance!
argumentsin thenotFoundmethodreturn target['notFound']you'd need toreturn function(..args) { target.notFound(prop, ...args) };or something like that. Or just store the accessedpropin a temporary property.