Proxy classes - nodirt/defineClass GitHub Wiki

Proxy classes

Generating a proxy

defineClass.proxy function generates a proxy class based on another class or a list of methods.

defineClass.proxy(class, fieldName="_real")
defineClass.proxy(arrayOfMethodNames, fieldName="_real")

Parameters class and arrayOfMethodNames specify methods to generate in the proxy class.

Parameter fieldName specifies a name of a field where the instance with a real implementation is stored.

var PhoneProxy = defineClass.proxy(Phone);
var phone = new Phone(1000, "120-1010");
var proxy = new PersonProxy(phone);
proxy.dial("120-0000"); // Output: Dialing to 120-0000

Inheriting from a proxy

Defining a class inherited from a proxy class is easy as

var PhoneWrapper = defineClass({
  _super: defineClass.proxy(Phone),

  dial: function (number) {
    this._super(number);
    console.log("too too");
  }
});

var phone = new Phone(1000, "120-1010");
var proxy = new PhoneWrapper(phone);
proxy.dial("120-0000"); // Output: Dialing to 120-0000\ntoo too