micro-namespace

Wrap object into a namespace

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
micro-namespace
0.2.14 years ago5 years agoMinified + gzip package size for micro-namespace in KB

Readme

micro-namespace #
----------------
Wrap libraries into a namespace.

clone ##

repository ####

$ hg clone https://bitbucket.org/frostbane/micro-namespace

npm ####

$ npm install micro-namespace

usage ##

AMD require ####

var ns = require("micro-namespace");

include script ####

<script type="text/javascript"
        src="micro-namespace.js"></script>

examples ####

simple namespace
// create fb.common.util namespace
namespace("fb.common.util");

// add methods to fb.common.util
fb.common.util.formatDate = function(...){ ... };
fb.common.util.logError   = function(...){ ... };

// use the methods/classes
if(!fb.common.util.formatDate("2017/12/14")){
    fb.common.util.logError("format failed");
}

namespace and object
// create fb.common.extra namespace with methods
namespace("fb.common.extra", {
    getDateNow   :function(){ ... },
    getServerUrl :function(){ ... },
    AjaxParser   :function(){ ... },
});

// use the methods/classes
var now = fb.common.extra.getDateNow();

var host = fb.common.extra.getServerUrl();

var ax = new fb.common.extra.AjaxParser();

amd require
var ns = require("micro-namespace");

ns("fb.ajax", {
    getUsername :function(){ ... },
});

var user = fb.ajax.getUsername();

initialize object
the third parameter (init) is called under the context of the second parameter (obj) after the namespace is created.
namespace("fb.common.upload", {
    uploadFile   :function(){},
    initUploader :function(){},
    resetErrors  :function(){},
}, function(){
    // exported methods can be accessed with this
    this.initUploader();
    
    this.resetErrors();
    
    $(function(){
        $("#cal").datepicker();
    });
});