Skip to content
Trait composition library for JavaScript
JavaScript CSS Other
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
licenses
scripts
support
test
.gitignore
.npmignore
.travis.yml
LICENSE.txt
README.md
bower.json
package.json
traits.js
traits.min.js

README.md

Build Status

Inactive

This repository is no longer actively maintained. Please use or create a fork.

Recommended: trait by YR.

N.B. since 2013, when I created this repository, traits-based composition seems to have lost none of its value as an alternative (or complement) to inheritance based programming in JavaScript. However, I found my needs better met by ClojureScript as I spent more time with that technology, and left off developing this library not long after I began working on it.

traits.js

Trait composition library for JavaScript.

Description

This library is a fork of the original traits.js by Tom Van Cutsem:

Traits are a flexible language feature to factor out and recombine reusable pieces of code. They are a more robust alternative to multiple inheritance or mixins. They are more robust because name clashes must be resolved explicitly by composers, and because trait composition is order-independent (hence more declarative). To put it simply: if you combine two traits that define a method with the same name, your program will fail. Traits won't automatically give precedence to either one.

Compatibility

For 1-to-1 features and compatibility with the original traits.js, including the built-in ECMAScript 5 shims, please install the latest 0.4.x release.

The Trait constructor is available on the object exported by the library:

var Trait = require("traits.js").Trait; // NodeJS

—or—

Trait = traits.Trait; // browser global scope

Goals

This library will share some similarities with another traits.js derivative, light-traits by Irakli Gozalishvili, e.g. ECMAScript 5 shims will (eventually) not be included in the core library. However, it's intended to be a fresh start built atop the same original.

By retooling and experimenting with the original traits.js, the primary goal is to come to a deeper understanding of the concepts, implementation, benefits and trade-offs of traits-based composition in JavaScript.

In time, this fork may explore the possibility of leveraging traits as a basis for gradual typing in JavaScript libraries, along the lines of Clojure's core.typed.

A more immediate goal is to leverage traits on top of Google Polymer, providing a mechanism for Custom Element composition which is complementary to prototypal inheritance, e.g. the extends feature of polymer-element. To that end, this library specifically targets bower, which presently seems to be the favored system for distributing Web Components and expressing dependencies in and between them.

Installation

You can install the latest release via bower:

$ bower install traits.js

It is also available through npm:

$ npm install traits.js

The installed package contains two consumable JavaScript files, traits.js and traits.min.js.

Usage

Load traits.js in your Node.js programs as you would any other module. The Trait constructor is available on the object exported by the library:

var Trait = require("traits.js").Trait;

In a Web browser, you can load traits.js with a <script> tag, as you would any other JavaScript library:

<script src="traits.min.js"></script>
<!-- `traits` is now in the global scope -->
<script>
  Trait = traits.Trait;
</script>

You can also load it as an AMD module, e.g. with RequireJS.

API and Examples

Documentation will be provided in the wiki. For the 0.4.x releases, the API will exactly match that of the original library, and the latter's documentation can be consulted:  API,  Tutorial,  HowToNode article,  PLASTIC Workshop 2011 paper.

Development and Testing

The npm and bower distributions are stripped down, so if you wish to hack on the library you should clone it:

$ git clone https://github.com/michaelsbradleyjr/traits.js
...
$ cd traits.js && npm install .

Testing with Node.js

Launch the mocha test runner with:

$ npm test

You can also have it run continuously, with re-runs triggered by changes to *.js files under the project's root.

$ npm run-script test-watch

Testing with Web browsers

To host the test suite locally, you will need a server — the ever handy http-server is highly recommended:

$ npm install -g http-server
...
$ http-server -p 12345 -c-1

Then point your browser/s to http://localhost:12345/test/  or  http://localhost:12345/test/amd.html.

You can also load the test suite as hosted on the gh-pages site for this repo:  script tag,  amd.

Bugs

You can report bugs and discuss features on the issues page or send tweets to @michaelsbradley.

Credit

This software is adapted from an existing work:  traits.js

For the original license text please refer to the licenses directory at the root of this distribution.

Copyright and License

This software is Copyright (c) 2013 by Michael Bradley, Jr.

The use and distribution terms for this software are covered by the Apache License, Version 2.0 which can be found in the file LICENSE.txt at the root of this distribution. By using this software in any fashion, you are agreeing to be bound by the terms of this license. You must not remove this notice, or any other, from this software.


Something went wrong with that request. Please try again.