I would like to present our readers a very interesting functionality that gives javascript developers a lot of power.

First for those who don’t know what the Proxy Pattern is, well a proxy(at function level) is a function that provides a interface to another function.

Let’s say we want to create a proxy for the console.log method. And have it do something else without altering it’s initial purpose.

Then by adding this code:

  1. (function() {
  2.     var proxied = console.log;
  3.     console.log = function() {
  4.        alert(arguments[0]);
  5.        return proxied.apply(this, arguments);
  6.   };
  7. })();

We will change the console.log function to also make an alert with the parameter it receives.
How do we do this?…Well we first wrap everything in a closure so we can have access to the ‘proxied’ variable. Then we save the console.log function in the closure and then we overwrite it. The proxy then alerts all data received when calling console.log and then delegates the call to the original function. Using apply(this, arguments) guarantees that the caller won’t be able to notice the difference between the original and the proxied method.