Traits library

Tom Van Cutsem tomvc at google.com
Tue Feb 16 18:29:21 PST 2010


Hi Erik,

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);
> ...
>

Your solution is almost right. If you want an object instantiated from a
trait to serve as the prototype of other objects, you should instantiate it
using "Object.create"
"Trait.create" will generate an object whose methods are 'bound' (i.e. their
"this"-binding is fixed). So, self-sends in this object won't delegate back
to "child" objects.

A good way to think of it is that Object.create generates 'plain' Javascript
objects whereas Trait.create generates 'final' objects.

Cheers,
Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20100216/47a45359/attachment.html>


More information about the es-discuss mailing list