revealing-module-factory-js
v1.0.2
Published
Revealing Module factory pattern.
Downloads
3
Readme
revealing-module-factory-js
A revealing module factory method for easily creating and using the revealing module pattern.
Features:
- Sets modules on itself (for easy access).
- Sets modules using
Object.defineProperty
(sets them as notwritable
and notconfigurable
). - Allows you to access your modules as properties; E.g.
myModule.all.your.base
etc.; - Allows you to fetch/set your modules using namespace strings when calling your
revealingModule
('all.your.base' etc.) when calling yourrevealingModule
as a function. - Less overhead when requesting your modules since they are just properties descending from your
revealingModule
(myModule.all.your.base
...). - Returned
revealingModule
can be called as a setter-getter (when called with one parameter - sets un-existing namespaces and returns final path in namespace), a getter (when called with one parameter - Returns existing namespaces) or as a setter (when called with 2 parameters - returns itself)
Usage:
Include './dist/revealingModuleFactory.js'
as part-of/in your project.
In your code:
const myModule = revealingModulePattern();
myModule instanceof Function === true; // true
// Now you can set namespaces on your module
const things = {},
thing = {};
// ""
myModule('thing', thing);
myModule.thing === thing; // true
// ""
myModule('all.the.things', things);
myModule.all.the.things === things; // true
// Once your namespaces are written they become unsettable
// (not `writable` or `configurable`)
myModule.all.the.things = function OtherThings() {};
// ""
myModule.all.the.things === things; // true
// Also calls to your module as a setter (called with
// namespace string and value) return itself
const multiple = {},
all = {},
once = {};
// ""
myModule('setting', multiple)
('things', all)
('at', once)
// Using myModule call as a getter
myModule('all.the.things') === things; // true
// Setting and getting a namespace all at once
myModule('all.your.base.are.belong.to.us');
// Note the initial 'all' in the previous example; It already
// exists (was set further up in the script) and gets reused -
// doesn't get recreated or overwritten.
Pre-requisites:
Es5+ Browsers
Tests:
$ npm install phantomjs -g
$ npm install
$ npm run tests
License:
GPL v2+ AND