Skip to content

Forward a list of methods to properties of an object on the fly (late-binding).

License

Notifications You must be signed in to change notification settings

sethbonnie/forward-props

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Forward a list of methods to properties of an object on the fly (late-binding).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published