Traits library

Erik Arvidsson erik.arvidsson at
Tue Feb 16 17:06:31 PST 2010

On Tue, Feb 16, 2010 at 16:57, Erik Arvidsson <erik.arvidsson at> wrote:
> One serious omission in your library is that traits cannot (could
> not?) be applied to existing objects. Adding traits to existing
> objects is important because it allows you to combine prototype based
> inheritance with traits by adding a trait to the prototype. Adding
> traits to the prototype, in turn makes composition a one time cost
> instead of per instance cost.
> function MyComponent() {}
> MyComponent.prototype = Object.create(MySuperClass.prototype);
> addTrait(MyComponent.prototype, eventTargetTrait);
> var c = new MyComponent;
> c.addEventListener('foo', bar);

I knew I should have looked at your updated library before posting. It
seems like you do support my scenario above by passing in the proto.

var EventTargetTrait = ...;

function MyComponent() {}
MyComponent.prototype = Trait.create(
    MySuperClass.prototype, EventTargetTrait);

I still have use cases where adding traits to existing objects would
be useful but those are more obscure and can be done by setting
__proto__ to a new object (shudders).


More information about the es-discuss mailing list