Code Monkey home page Code Monkey logo

light-traits's Introduction

light-traits

Very light trait composition library for Javascript based on the new object manipulation API defined in Ecmascript-262, Edition 5

Install

npm install selfish

Require

var Trait = require('https!raw.github.com/Gozala/light-traits/v0.2.0/light-traits.js').Trait

Traits

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.

Light Traits

This library is inspired by an awesome traitsjs and pretty much follows it's design with a few slight differences:

Lighter

This implementation is more optimal in terms of object instantiation speed and memory footprint. This is achieved by not binding this pseudo variable in all accessors and methods of the composed object to an instance, which allows shared functions across objects, it also matches better overall behavior of the language. (BTW Similar can be achieved in traitjs as well in which case the claim of being more optimal will not longer be true, it's just not a default behavior at least not yet :)

Better ergonomics

This is a big statement and maybe not entirely correct but, few API differences makes working with traits less verbose and there for more pleasant. See examples section to find a difference.

Ecmascript 5 shims are not included

If your JavaScript engine does not comes with all the shiny Ecmascript 5 features this library is not going to work, luckily there are other libraries that shim JavaScript engines so that might be an option to go with.

Examples

var Trait = require('https!raw.github.com/Gozala/light-traits/v0.2.0/light-traits.js').Trait
  function ColorTrait(color) {
    return Trait({ color: function() { return color } })
  }
  var pointTrait = Trait.compose(
    ColorTrait('red'), // a color trait
    Trait({
      _x: Trait.required,
      _y: Trait.required,
      get x() { return this._x },
      get y() { return this._y },
      toString: function toString() { return '' + this.x + '@' + this.y }
    })
  )
  function Point(x, y) {
    // result inherits from object being passed to create
    return pointTrait.create({ _x: x, _y: y })
  }
  var point = Point(1, 7)
  point.color() // -> 'red'
  point.toString() // -> 1@7

light-traits's People

Contributors

gozala avatar sundippatel avatar

Stargazers

Jeremy Sarda avatar Heroselohim avatar wadi avatar Yoshiya Hinosawa avatar Jaime avatar 墨神 avatar Michał Kowol avatar Preston Parris avatar Guillaume Marceau avatar David Weinstein avatar  avatar Ian Irvine avatar David Hayes avatar Mark Stosberg avatar Michael Bradley avatar Sylvain Vuilliot avatar Matt Zukowski avatar  avatar Brad Pillow avatar hui avatar HE Shi-Jun avatar Yi Wang avatar Mike de Boer avatar ROHITHA DASSANAYAKE avatar Shaun Trennery avatar lifesinger avatar Allen Madsen avatar  avatar  avatar C Dorn avatar James Abley avatar Jesse Hallett avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

mikedeboer hax yr jkso

light-traits's Issues

Add a inherits method to complement util.inherits

Hi,

I'm looking for a traits implementation in JS and found this one is pretty interesting.
As you know, Node.js has a util.inherits method to simplify inheritance.

In terms of traits, it seems we need a similar util function that can combine util.inherits and traits together, that's what I have written.
I use lodash extend method for simplicity.

function inherits(constructor, parentConstructor, trait, properties) {
  util.inherits(constructor, parentConstructor);
  if (properties !== undefined)
    _.extend(constructor.prototype, properties);
  if (trait !== undefined)
    constructor.prototype = trait.create(constructor.prototype);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.