DailyJS

DailyJS

The JavaScript blog.


Tagorganisation
Featured

libraries organisation

Traits

Posted on .

Traits.js (Apache License 2.0) is a library for creating modular components using
traits. Traits can be used to recombine reusable code in a similar way to mixins
without introducing traditional classes. Within the library, traits are
like JavaScript functions, in that they're first-class and anonymous.

Trait.compose can be used to combine a set of traits, which
can then be instantiated with Trait.create. From the
documentation:

Create is similar to the Ecmascript 5 built-in Object.create except that it generates high-integrity, "final" objects.

It also ensures the resulting object is frozen, and this is
bound to the instantiated object in all accessors and methods.

As you can see by what create does, this process generates
objects that are more strictly controlled than mixin or multiple
inheritance-based code.

The tutorial
has a good example called An Enumerable Trait which helped me get the
hang of the basic usage of the library.