The Proxy Pattern in Javascript

The jQuery function documentation has all kinds of great tidbits it. One new thing I discovered is the Proxy Pattern.

The idea of the proxy pattern is that you wrap a function around an existing function. This lets you perform additional actions before the wrapped function is called while the modified function appears exactly the same to the caller.

Here's a simple example from the documentation

~~~~ {.javascript name="code"} (function() { // log all calls to setArray var proxied = jQuery.fn.setArray; jQuery.fn.setArray = function() { console.log(this, arguments); return proxied.apply(this, arguments); };})(); wzxhzdk:0

In the above case, we're wrapping the alert. We check to see if any sort of console object is available. If a console is defined, we output the value passed to alert to the console instead of displaying the Javascript alert box. If no console is defined, we call the alert function normally. We have to include an additional check of the apply function because window.alert doesn't have an apply method in Internet Explorer 8.