Proxy traits - nodirt/defineClass GitHub Wiki

defineClass.proxyTrait(classOrArrayOfMethods, fieldName) is like [defineClass.defineProxy](Proxy classes) except it generates a trait instead of class.

One of its application is method delegation to an object stored in a field:

var Foo = defineClass({
  doFoo: function () { /*...*/ }
});

var Baz = defineClass({
  _super: [
    defineClass.proxyTrait(Foo, "_foo")
  ],

  constructor: function () {
    this._foo = new Foo();
  }
});

var baz = new Baz();
baz.doFoo();

Here Baz class has a Bar instance in the _bar field. By mixing the proxy trait the Baz class gets doBar method that calls doFoo method of the object in the _foo field.

You can apply as many proxy traits as you want

var Bar = defineClass({
  doBar: function () { /*...*/ }
});

var Baz = defineClass({
  _super: [
    defineClass.proxyTrait(Foo, "_foo"),
    defineClass.proxyTrait(Bar, "_bar")
  ],

  constructor: function () {
    this._foo = new Foo();
    this._bar = new Bar();
  }
});

var baz = new Baz();
baz.doFoo();
baz.doBar();