bilbo
Bilbo is a simple dependency injection library for JavaScript. It's intended audience is for node-js applications, but it can also be used for client-side applications.
He uses bags to store and distribute dependencies along your code. Bags are identifies by it's name within bilbo.
var bilbo = ;var bag = bilbo;
If there is no bag with a given name, bilbo.bag()
will create one.
Fetching things from a bag is done by the bag.grab()
method. Each thing is identified by it's name within the bag.
var thing = bag;
Bags can store things in many ways:
- stuff: simple storage, no action performed
- prototype: stores a prototype object used to create new objects having this one as prototype
- factory: stores a function to be used as factory each time
- lazy: stores a function that initializes an returns something that may be returned every time
- type: stores a constructor function that may be used for creating new instances each time
- singleton: stores a constructor function that may be used for create a single instance
stuff
Just stores something within the bag itself. No modification whatsoever is taken upon the stored data.
var myStuff = {}; // stores myStuff under "stuff's name"bag; // grabs back myStuff using "stuff's name"var myStuffBack = bag; myStuff === myStuffBack; // true
prototype
Stores a prototype object. This object will be used as prototype for a newly created object each time bag.grab()
is called.
var myPrototype = a: {} ;// stores myPrototype under "prototype's name"bagprototype"prototype's name" myPrototype; // grabs a new object having myPrototype as prototype using "prototype's name"var myObject = bag; myObjecta === myPrototypea; // truemyObject; // false
factory
Stores a factory function. This function will be used to create new things each time bag.grab()
is called.
var { return {}; };// stores myFactory under "factory's name"bag; // grabs oneThing created by the myFactory functionvar oneThing = bag;// grabs anotherThing created by the myFactory functionvar anotherThing = bag; oneThing === anotherThing; // false
lazy
Stores a lazy function. This function will be used only once to create a new thing when bag.grab()
is called. Subsequent calls will return the same thing created the first time it was called.
var { return {}; };// stores myLazy under "lazy's name"bag; // grabs a thing created by the myLazy functionvar thing = bag;// grabs the sameThing created by the myLazy function previouslyvar sameThing = bag; thing === sameThing; // true
type
Stores a type constructor function. This function will be used as a constructor to create new type instances each time bag.grab()
is called.
var {};// stores MyConstructor under "type's name"bagtype"type's name" MyConstructor; // grabs anInstance created by the MyConstructor functionvar anInstance = bag;// grabs anotherInstance created by the MyConstructor functionvar anotherInstance = bag; anInstance === anotherInstance; // falseanInstance instanceof MyConstructor; // trueanotherInstance instanceof MyConstructor; // true
singleton
Stores a type constructor function as a singleton. This function will be used as a singleton constructor to create a singleton instance once bag.grab()
is called. Subsequent calls yields the same singleton instance.
var {};// stores MySingletonConstructor under "singleton's name"bag; // grabs aSingleton created by the MySingletonConstructor functionvar aSingleton = bag;// grabs sameSingleton created by the MySingletonConstructor function previouslyvar sameSingleton = bag; aSingleton === sameSingleton; // trueaSingleton instanceof MySingletonConstructor; // true