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
};
})();