node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org ยป


The summary

Base is a lightweight framework for creating consistent, highly readable, chainable and inheritable object prototypes in Javascript. This git repo is for an NPM module for use with Node. It can be used in the browser so long as you manually satisfy the Underscore dependency. You're already using Underscore anyway though, right, so no problem, yeah?


The detail

So, here's some examples and stuff.


npm install base-framework


// Require base...
var base = require('base');

// create a base child. Add the default 'init' constructor which is
// executed automatically when the object is invoked, and another 
// instance method.

var MyFactory = base.createChild()
		init : function( args ){
			this.args = args;
			return this;
		spewArgs : function(){

var myInstance = MyFactory({ hello : 'world'}).spewArgs() // outputs {hello : 'world'} to the console

Objects created with base.createChild() have a number of static methods which are used to build your factory.


var Foo = base.createChild();

	computeSecretOfEverything : function(){
		this.secret = 42;
		return this;
	getSecret : function(){
		return this.secret;

Bar = Foo();

Bar.getSecret() === 42 // TRUE

When your instance methods return this instead of a value, you can chain the methods, jQuery style, instead.



Static methods are only available on the object created with base.createChild(), not an instance.

var Foo = base.createChild();

	secretOfLifeUniverseAndEverything : function(){
		return 42;

Foo.secretOfLifeTheUniverseAndEverything() === 42 // TRUE


Any object created with base.createChild() also has the createChild method, which means you can create a whole bunch of instance and static methods on one child of base, then inherit (and override) those later on.

var Horse = base.createChild()
		name : function(){
			return "Horse"
		legs : function(){
			return 4;
		goFaster : function(){

var Unicorn = Horse.createChild()
		name : function(){
			return "Unicorn";
		kill : function(){
			// extra functionality available to unicorns

var uni = Unicorn();
var bill = Horse();

uni.legs() === 4 // TRUE === 'Unicorn' // TRUE
uni.goFaster() // exists

bill.kill() // doesn't exist


make test