1.0.0 • Published 7 years ago

dtx-base v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

dtx-base

About

Base module to simplify IIFE-centric modularization.

Background

Before ES2015 and modularization models like AMD and CommonJS dealing with global scope as well as containing functions into some kind of entity, i.e. modularization, was achieved by use of Immediately-Invoked Function Expressions (IIFE:s).

The dtx-base simplifies IIFE-centric modularization and creation of namespaces.

Description

To instantiate a module simply call the BaseModule constructor with first parameter being the name of the module and the optional second parameter being an already existing namespace object.

Implementation

  1. Add the file dtx-base.js as a dependency.
  2. Now you are ready to implement.
/**
 * EXAMPLE 1: Create simple namespace.
 */
// Create namespace.
(function (self) {
    self.hi = hi;

    function hi() {
        console.info("Hi from example 1!");
    }
})(new BaseModule("SimpleRoot"));

// Namespace is now modularized...
// ...and the exposed hi function is able to implement:
SimpleRoot.hi();
/**
 * EXAMPLE 2: Create namespace with root and child.
 */
// Create root namespace.
var ParentRoot = new BaseModule("ParentRoot");

// Create child namespace.
(function (self) {
    "use strict";

    self.hi = hi;

    function hi() {
        console.info("Hi from example 2!");
    }
})(new BaseModule("Child", ParentRoot));

// Child namespace is now modularized within the context of the ParentRoot namespace.
// Implement the exposed hi function like this:
ParentRoot.Child.hi();

Author

DevTactix

1.0.0

7 years ago