Giter Site home page Giter Site logo

simple-traits's Introduction

A simple trait composition library based on Object.create. This is a fork of light-traits to improve overall flexibility, as well as add basic polyfills for Object.create and property descriptor handling.

Usage

var Trait = require('trait');

var T = Trait.compose(
  require('../shared/baseTrait'),
  Trait({
    someProp: Trait.required,
    someMethod: function () {
      console.log(this.someProp + ' world!');
    }
  })
);

module.exports = function () {
  return T.create(Object.prototype, {
    someProp: 'hello'
  });
};

Traits

Traits are a simple mechanism for representing reusable and composable functionality. They are more robust alternatives to mixins and multiple inheritance because name clashes must be explicitly resolved, and because composition is commutative and associative (ie. the order of traits in a composition is irrelevant).

Use traits to share functionality between similar objects without duplicating code or creating complex inheritance chains.

Trait Creation

To create a trait, call the Trait factory function exported by this module, passing it an object that specifies the properties of the trait.

var t = Trait({
  foo: Trait.required,
  bar: function bar () {
    return this.foo;
  },
  baz: 'baz'
});

Traits can both provide and require properties. A provided property is a property for which the trait itself provides a value. A required property is a property that the trait needs in order to function correctly but for which it doesn't provide a value.

Required properties must be provided by another trait or by an object with a trait. Creation of an object with a trait will fail if required properties are not provided. Specify a required property by setting the value of the property to Trait.required.

Object Creation

Create objects with a single trait by calling the trait's create method. The method takes two arguments, the object to serve as the new object's prototype, and an optional object defining properties of the new object. If no prototype is specified, the new object's prototype will be Object.prototype.

var myTrait = Trait({
  foo: 'foo',
  bar: 2
});
var foo1 = t.create();
var foo2 = t.create(Object.prototype, {
  baz: 'baz'
});

Trait Composition

Traits are designed to be composed with other traits to create objects with the properties of multiple traits. To compose an object with multiple traits, you first create a composite trait and then use it to create the object. A composite trait is a trait that contains all of the properties of the traits from which it is composed.

var tBase = Trait({
  foo: Trait.required,
  id: function () {
    return this.foo;
  }
});
var tBaseFoo = Trait({
  foo: 'foo'
});

var tFoo = Trait.compose(tBase, tBaseFoo);

Trait Resolution

Composite traits have conflicts when two of the traits in the composition provide properties with the same name but different values (when compared using the === strict equality operator).

var t1 = Trait({
  foo: 'foo',
  bar: 'bar'
});
var t2 = Trait({
  bar: 'foo',
});

var tc = Trait.compose(t1, t2); => Error 'remaining conflicting property'

Attempting to create an object from a composite trait with conflicts throws a remaining conflicting property exception. To create objects from such traits, you must first resolve the conflict.

Conflit resolution is achieved by excluding or renaming the conflicting property of one of the traits. Excluding a property removes it from the composition, so the composition only acquires the property from the other trait. Renaming a property gives it a new, non-conflicting name at which it can be accessed.

In both cases, you call the resolve method on the trait whose property you want to exclude or rename, passing it an object. Each key in the object is the name of a conflicting property: each value is either null to exclude the property, or a string representing the new name of the property.

For example, the conflict in the previous example could be resolved by excluding the bar property of the first trait:

var tc = Trait(t1.resolve({bar: null}), t2);

It could also be resolved by renaming the bar property of the first trait:

var tc = Trait(t1.resolve({bar: 'bar2'}), t2);

When you resolve a conflict, the same-named property of the other trait (the one that wasn't excluded or renamed) remains available in the composition under its original name.

simple-traits's People

Contributors

gozala avatar popeindustries avatar sundippatel avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

simple-traits's Issues

TypeError: Cannot read property 'value' of undefined index.js:238

Hi I was trying out this library and following one of the simple examples from the read me and its throwing an error.

Heres my script.

var Trait = require('trait');

var t = Trait({
  foo: Trait.required,
  bar: function bar () {
    return this.foo;
  },
  baz: 'baz'
});

And heres the error

/Users/kevzettler/VoxLords/node_modules/trait/index.js:238
        attr._origin = attr.value;
                           ^
TypeError: Cannot read property 'value' of undefined
    at Object.Attributes.add (/Users/kevzettler/VoxLords/node_modules/trait/index.js:238:28)
    at Object.Attributes (/Users/kevzettler/VoxLords/node_modules/trait/index.js:27:14)
    at trait (/Users/kevzettler/VoxLords/node_modules/trait/index.js:10:12)
    at Object.<anonymous> (/Users/kevzettler/VoxLords/traittest.js:3:9)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

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.