js模块代码

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
};
})();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值