Proxy

In general, a proxy is an object that controls access to another object (referred to as the subject). It has exactly the same interface as the subject and does not modify the subject’s behaviour. All it does is forward method calls onto the subject and it returns the results of such calls. Proxies are often used to restrict access to a subject, to provide a local interface to a remote object (such as a web service API), or to allow the instantiation of memory-intensive objects on demand.

// In the browser
JS.require('JS.Proxy', function(Proxy) { ... });

// In CommonJS
var Proxy = require('jsclass/src/proxy').Proxy;

Virtual proxies

A virtual proxy is an object that acts as a stand-in for its subject. The subject is not initialized until it is really needed, allowing for ‘lazy instantiation’ of objects that are expensive to create.

jsclass provides a module called Proxy.Virtual. This allows you create proxies for classes without needing to write all the forwarding and instantiation methods yourself. Proxy.Virtual inspects the proxied class and automatically creates proxies for all its instance methods. This saves you time and reduces code duplication.

Consider the following example: we have a Dog class, which keeps track of how many times it has been instantiated through its class property instances. We then create a DogProxy class from Dog, and instantiate it. At this point we see that Dog.instances == 0. Then we call rex.bark(), which instantiates rex’s subject and calls bark() on it. Dog.instances now equals 1. Calling further methods on rex will not create any more Dog instances.

var Dog = new Class({
    extend: {
        instances: 0
    },
    initialize: function(name) {
        this.name = name;
        this.klass.instances++;
    },
    bark: function() {
        return this.name + ' says WOOF!';
    }
});

var DogProxy = new Proxy.Virtual(Dog);

var rex = new DogProxy('Rex');
Dog.instances   // -> 0

rex.bark()  // -> "Rex says WOOF!" 
Dog.instances   // -> 1

This pattern is particularly suited to creating parts of a UI that are initially hidden – you can create proxies for them on page load, but they won’t add any of their HTML to the page until you choose to show them.