Skip to content

Base Module to simplify IIFE-centric modularization.

License

Notifications You must be signed in to change notification settings

DevTactix/dtx-base

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Base Module to simplify IIFE-centric modularization.

Resources

License

Stars

Watchers

Forks

Packages

No packages published