js模块代码
来源:互联网 发布:小学生网络礼仪 编辑:程序博客网 时间:2024/05/04 04:36
var Module;
if(Module && typeof Module != "object") {
throw new Error("Namespace 'Module' already exists.");
}
Module = (function() {
var global = (function() {return this;}).call(null);
var modules = {"Module" : {"NAME" : "Module", "VERSION" : 1.0, "EXPORT" : ["require", "importSymbols"], "EXPORT_OK" : ["createNamespace", "isDefined", "registerInitializationFunction", "runInitializationFunctions"]}};
var initfuncs = [];
var createNamespace = function(name, version) {
if(!name) {
throw new Error("Namespace name required.");
}
if(!(/^[a-zA-Z][a-zA-Z0-9]*(\.[a-zA-Z][a-zA-Z0-9]*)*$/.test(name))) {
throw new Error("Namespace name '" + name + "' is illegal.");
}
var parts = name.split("."),
container = global,
i = 0,
j = 0;
for(i = 0, j = parts.length; i < j; i++) {
var part = parts[i];
if(!container[part]) {container[part] = {};}
if(typeof container[part] != "object") {
var e = parts.slice(0, i + 1).join(".");
throw new Error("'" + e + "' alreay exists and is not an object.");
}
container = container[part];
}
var namespace = container;
if(namespace.NAME) {
throw new Error("Model '" + name + "' is alreay defined.");
}
namespace.NAME = name;
if(version) {
namespace.VERSION = version;
}
modules[name] = namespace;
return namespace;
};
var isDefined = function(name) {
return name in modules;
};
var require = function(name, version) {
if(!(name in modules)) {
throw new Error("Module '" + name + "' is not defined.");
}
if(!version) {
return;
}
var module = modules[name];
if(module.VERSION && module.VERSION < version) {
throw new Error("Module '" + name + "' has version " + module.VERSION + " but version " + version + " or greater is required");
}
};
var importSymbols = function(module) {
if(typeof module == "string") {
module = modules[module];
}
if(!module || typeof module != "object") {
throw new Error("Module object is not defined.");
}
var to = global,
symbols = [],
symbolFlag = 1,
i = 0,
j = 0;
if(arguments.length > 1 && typeof arguments[1] == "object") {
if(arguments != null) {
to = arguments[1];
}
symbolFlag = 2;
}
for(i = symbolFlag, j = arguments.length; i < j; i++) {
symbols.push(arguments[i]);
}
if(symbols.length == 0) {
if(module.EXPORT) {
for(i = 0, j = module.EXPORT; i < j; i++) {
v = module.EXPORT[i];
to[v] = module[v];
}
} else if(!module.EXPORT_OK) {
for(v in module) {
to[v] = module[v];
}
}
} else {
var allowed;
if(module.EXPORT || module.EXPORT_OK) {
allowed = {};
if(module.EXPORT) {
for(i = 0, j = module.EXPORT.length; i < j; i++) {
allowed[module.EXPORT[i]] = true;
}
}
if(module.EXPORT_OK) {
for(i = 0, j = module.EXPORT_OK.length; i < j; i++) {
allowed[module.EXPORT_OK[i]] = true;
}
}
}
for(i = 0, j = symbols.length; i < j; i++) {
var s = symbols[i];
if(!(s in module)) {
throw new Error("Symbol '" + s + "' is not defined.");
}
if(allowed && !(s in allowed)) {
throw new Error("Symbol '" + s + "' is not public and cannot to import.");
}
to[s] = module[s];
}
}
};
var runInitializationFunctions = function() {
for(var i = 0, j = initfuncs.length; i < j; i++) {
try {
initfuncs[i]();
} catch (e) {}
}
initfuncs.length = 0;
};
var registerEventHandler = function() {
var isRunBroswer = "window" in global && "navigator" in global["window"];
if(isRunBroswer) {
if(window.addEventListener) {
window.addEventListener("load", runInitializationFunctions, false);
} else if(window.attachEvent) {
window.attachEvent("onload", runInitializationFunctions);
} else {
window.onload = runInitializationFunctions;
}
}
};
var registerInitializationFunction = function(f) {
initfuncs.push(f);
registerEventHandler();
};
return {
"createNamespace" : createNamespace,
"isDefined" : isDefined,
"require" : require,
"importSymbols" : importSymbols,
"registerInitializationFunction" : registerInitializationFunction,
"runInitializationFunctions" : runInitializationFunctions
};
})();
if(Module && typeof Module != "object") {
throw new Error("Namespace 'Module' already exists.");
}
Module = (function() {
var global = (function() {return this;}).call(null);
var modules = {"Module" : {"NAME" : "Module", "VERSION" : 1.0, "EXPORT" : ["require", "importSymbols"], "EXPORT_OK" : ["createNamespace", "isDefined", "registerInitializationFunction", "runInitializationFunctions"]}};
var initfuncs = [];
var createNamespace = function(name, version) {
if(!name) {
throw new Error("Namespace name required.");
}
if(!(/^[a-zA-Z][a-zA-Z0-9]*(\.[a-zA-Z][a-zA-Z0-9]*)*$/.test(name))) {
throw new Error("Namespace name '" + name + "' is illegal.");
}
var parts = name.split("."),
container = global,
i = 0,
j = 0;
for(i = 0, j = parts.length; i < j; i++) {
var part = parts[i];
if(!container[part]) {container[part] = {};}
if(typeof container[part] != "object") {
var e = parts.slice(0, i + 1).join(".");
throw new Error("'" + e + "' alreay exists and is not an object.");
}
container = container[part];
}
var namespace = container;
if(namespace.NAME) {
throw new Error("Model '" + name + "' is alreay defined.");
}
namespace.NAME = name;
if(version) {
namespace.VERSION = version;
}
modules[name] = namespace;
return namespace;
};
var isDefined = function(name) {
return name in modules;
};
var require = function(name, version) {
if(!(name in modules)) {
throw new Error("Module '" + name + "' is not defined.");
}
if(!version) {
return;
}
var module = modules[name];
if(module.VERSION && module.VERSION < version) {
throw new Error("Module '" + name + "' has version " + module.VERSION + " but version " + version + " or greater is required");
}
};
var importSymbols = function(module) {
if(typeof module == "string") {
module = modules[module];
}
if(!module || typeof module != "object") {
throw new Error("Module object is not defined.");
}
var to = global,
symbols = [],
symbolFlag = 1,
i = 0,
j = 0;
if(arguments.length > 1 && typeof arguments[1] == "object") {
if(arguments != null) {
to = arguments[1];
}
symbolFlag = 2;
}
for(i = symbolFlag, j = arguments.length; i < j; i++) {
symbols.push(arguments[i]);
}
if(symbols.length == 0) {
if(module.EXPORT) {
for(i = 0, j = module.EXPORT; i < j; i++) {
v = module.EXPORT[i];
to[v] = module[v];
}
} else if(!module.EXPORT_OK) {
for(v in module) {
to[v] = module[v];
}
}
} else {
var allowed;
if(module.EXPORT || module.EXPORT_OK) {
allowed = {};
if(module.EXPORT) {
for(i = 0, j = module.EXPORT.length; i < j; i++) {
allowed[module.EXPORT[i]] = true;
}
}
if(module.EXPORT_OK) {
for(i = 0, j = module.EXPORT_OK.length; i < j; i++) {
allowed[module.EXPORT_OK[i]] = true;
}
}
}
for(i = 0, j = symbols.length; i < j; i++) {
var s = symbols[i];
if(!(s in module)) {
throw new Error("Symbol '" + s + "' is not defined.");
}
if(allowed && !(s in allowed)) {
throw new Error("Symbol '" + s + "' is not public and cannot to import.");
}
to[s] = module[s];
}
}
};
var runInitializationFunctions = function() {
for(var i = 0, j = initfuncs.length; i < j; i++) {
try {
initfuncs[i]();
} catch (e) {}
}
initfuncs.length = 0;
};
var registerEventHandler = function() {
var isRunBroswer = "window" in global && "navigator" in global["window"];
if(isRunBroswer) {
if(window.addEventListener) {
window.addEventListener("load", runInitializationFunctions, false);
} else if(window.attachEvent) {
window.attachEvent("onload", runInitializationFunctions);
} else {
window.onload = runInitializationFunctions;
}
}
};
var registerInitializationFunction = function(f) {
initfuncs.push(f);
registerEventHandler();
};
return {
"createNamespace" : createNamespace,
"isDefined" : isDefined,
"require" : require,
"importSymbols" : importSymbols,
"registerInitializationFunction" : registerInitializationFunction,
"runInitializationFunctions" : runInitializationFunctions
};
})();
- js模块代码
- js注册模块代码优化
- Node.js 用模块组织代码
- 代码干货 | JS模块标准怎么这么多?
- 【Js--代码模块】最精练的div移动代码
- 【Js--代码模块】多附件上传和修改附件
- 浅析 Node.js 的 vm 模块以及运行不信任代码
- js模块
- js 模块
- js模块
- 门外汉学Web开发-WordPress代码导读(4-Admin后台模块之JS)
- node.js require 实现机制初窥;pomelo代码分析5----------- pomel-loader模块
- 前端页面文件拖拽上传模块html/css/js代码示例
- html中实现js调用 IOS Android 模块 | html js IOS Android 数据交互 | web端代码
- 前台JS模块设计
- js 隐藏/显示模块
- JS模块模式
- js模块的实现
- 组织开展“人民调解员业务技能竞赛活动”的实施方案
- ioctl和struct ifreq
- 【100题】二叉排序树转成双向链表----中序遍历法
- 模板小结
- c#下MD5加密算法
- js模块代码
- Linux添加PATH,即命令自动补全功能
- js浏览器信息获取
- 音视频格式家族
- myeclipse下安装svn插件方法
- 进程间通信
- 代理模式
- A DBA’s take on MSCA (Mobile supply chain applications)
- linux环境进程间通信