forward-props

0.3.1 • Public • Published

forward-props

Forward a list of properties to providers for an object on the fly (late-binding). The main usage is to have an object that provides an interface to its internal object and lets them handle the details of each of the methods. This is really useful when you want an object to behave like very disparate objects, yet don't want to mix those into one object with mixins or inheritance.

Install

$ npm install forward-props

Usage

var forward = require( 'forward-props' );
 
// calculator interface
var calc = Object.create( null );
 
// private addition module
var _addition = {
  add: function(a,b) { return a + b },
  sum: function( _args ) {
    var args = __slice.call( arguments, 0 );
    return args.reduce( this.add, 0 );
  }
};
 
// private multiplication module
var _multiplication = {
  multiply: function( a, b ) { return a * b },
  product: function( _args ) {
    var args = __slice.call( arguments, 0 );
    return args.reduce( this.multiply, 1 );
  }
};
 
// provide calc with addition properties
forward( calc, ['add', 'sum'], _addition );
 
// a little multiplication wouldn't hurt
forward( calc, ['multiply', 'product'], _multiplication );
 
calc.sum(1,2,3,4,5);      // 15
calc.product(1,2,3,4,5);  // 120

Readme

Keywords

none

Package Sidebar

Install

npm i forward-props

Weekly Downloads

1

Version

0.3.1

License

none

Last publish

Collaborators

  • sethbonnie