javascript namespace library
名前 | サイズ | Rev. | 日時 | 作者 | ログメッセージ |
---|---|---|---|---|---|
.hgignore | 85 | a61e9ad47101 | 2017-12-15 12:47:02 | 繧「繝ォ萓ソ <aru.bin@takyu.bin> | add initial code |
.hgtags | 48 | ae52b5943c5e | 2017-12-15 12:45:09 | 繧「繝ォ萓ソ <aru.bin@takyu.bin> | add V0.0.0 tag to 65b277bb3d5e |
Gruntfile.js | 6.77 k | 893be84dc4d5 | 2017-12-15 19:44:01 | 繧「繝ォ萓ソ <aru.bin@takyu.bin> | create unittest |
LICENSE.md | 5.4 k | 490e0a445f71 | 2018-12-24 15:05:43 | frostbane <frostbane@programmer.net> | update version |
README.md | 1.8 k | 1ad0daac98f2 | 2018-12-24 15:01:40 | frostbane <frostbane@programmer.net> | add init argument |
package.json | 793 | 1ad0daac98f2 | 2018-12-24 15:01:40 | frostbane <frostbane@programmer.net> | add init argument |
src | a28bac94f141 | 2019-08-07 12:05:26 | frostbane <frostbane@programmer.net> | add comments | |
test | 1ad0daac98f2 | 2018-12-24 15:01:40 | frostbane <frostbane@programmer.net> | add init argument |
Wrap libraries into a namespace.
$ hg clone https://bitbucket.org/frostbane/micro-namespace
$ npm install micro-namespace
var ns = require("micro-namespace");
<script type="text/javascript"
src="micro-namespace.js"></script>
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();
});
});