It's important to note that the this
within your apply function is referring to its own scope, specifically the function itself.
You have the option to pass parameters to the apply function as shown below:
apply: function(target, that, args) { ... }
In this context, the target
represents the bar function, the that
is a reference to the parent object, and args
is... well, you can probably guess :-)
class foo {
x = 10;
bar(value) {
console.log('Class variable x: ', x);
console.log('Method Parameter: ', value)
}
}
foo.prototype["_bar"] = foo.prototype.bar;
foo.prototype.bar = new Proxy(foo.prototype.bar, {
apply: function(target, that, args) {
console.log("Target", target);
console.log("THAT", that);
console.log("args", args);
}
});
new foo().bar('World');
If you were to call target.bar(args)
within your apply function, it would result in an infinite loop.