模块创建,检查工具方法(来自犀牛书)

 

 

var Module;
if (Module && (typeof Module != "object" || Module.NAME))
	throw new Error("Namespace 'Module' already exists");

Module = {};

Module.Name = 'Module';
Module.Version = 0.1;

Module.EXPORT = [ "require", "importSymbols" ];

Module.EXPORT_OK = [ "createNamespace", "isDefined",
                     "registerInitializationFunction",
                     "runInitializationFunctions",
                     "modules","globalNameSpace"];

Module.globalNamespace = this;

Module.modules = {"Module" : Module};

Module.createNamespace = function(name,version){
	if(!name) throw new Error("Module.createNamespace(): name required");
	if(name.charAt(0)=='.'||
	   name.charAt(name.length-1)=='.'||
	   name.indexOf("..")!=-1)
		throw new Error("Module.createNamespace(): Illegal name:" + name);
	
	var parts = name.split('.');
	var container = Module.globalNamespace;
	
	for(var i =0; i <parts.length;i++){
		var part = parts[i];
		if(!container[part]) container[part] = {};
		else if(typeof container[part]!="object"){
			var n = parts.slice(0,i).join('.');
			throw new Error(n + "already exists and is not an object");
		}
		container = container[part];
	}
	
	var namespace = container;
	if(namespace.NAME) throw new Error("Module " + name + " is already defined");
	
	namespace.NAME = name;
	
	if(version) namespace.VERSION = version;
	
	Module.modules[name] = namespace;
	
	return namespace;
}

Module.isDefined = function(name){
	return name in Module.modules;
}

Module.require = function(name,version){
	if(!(name in Module.modules)){
		throw new Error("Module " + name + " is not defined");
	}
	
	if(!version) return;
	
	var n = Module.modules[name];
	
	if(!n.VERSION || n.VERSION < version){
	throw new Error("Module " + name + " has version " + n.VERSION + " but version " + version + "or greater is required.");
	}
}

Module.importSymbols = function(from){
	if(typeof from == "string") from = Module.modules[from];
	if(!from || typeof from !="object"){
		throw new Error("Module.importSymols(): " + "namespace object required");
	}
	var to = Module.globalNamespace;
	var symbols = [];
	var firstSymbol = 1;
	
	if(arguments.length>1 && typeof arguments[1] =="object"){
		if(arguments[1]!=null) to = arguments[1];
		firstSymbol = 2;
	}
	
	for(var a = firstSymbol; a < arguments.length; a++){
		symbols.push(arguments[a]);
	}
	
	if(symbols.length==0){
		if(from.EXPORT){
			for(var i = 0; i < from.EXPORT.length; i++){
				var s = from.EXPORT[i];
				to[s] = from[s];
			}
			return
		}
		else if(!from.EXPORT_OK){
			for(s in from){
				to[s] = from[s];
				return;
			}
		}
	}
	var allowed;
	if(from.EXPORT || from.EXPORT_OK){
		allowed = {};
		if(from.EXPORT)
			for(var i = 0; i < from.EXPORT.length; i++)
				allowed[from.EXPORT[i]] = true;
		if(from.EXPORT_OK)
			for(var i = 0; i < from.EXPORT_OK.length; i++)
				allowed[from.EXPORT_OK[i]] = true;
	}
		
	for(var i = 0; i <symbols.length; i++){
		var s = symbols[i];
		if(!(s in from)) throw new Error("Module.importSymbols(): symbol" + s +" is not defined");
		if(allowed && !(s in allowed)) throw new Error("Module.importSymbols(): symbol" + s +" is not public and cannot be imported.");
		to[s] = from[s];
	}
}

Module.registerInitializationFunction = function(f){
	Module._initfuncs.push(f);
	Module._registerEventHandler();
}

Module.runInitializationFunctions = function(){
	for(var i = 0; i < Module._initFuncs.length; i++){
		try{ Module._initFuncs[i](); }
		catch(e){/* ingore */}
	}
	Module._initFuncs.length = 0 ;
}

Module._initFuncs = [];

Module._registerEventHandler = function(){
	var clientside = "window" in Module.globalNamespace && "navigator" in window;
	if(clientside){
		if(window.addEventListener){
			window.addEventListener("load",Module.runInitializationFunctions,false);
		}
		else if(window.attachEvent){
			window.attachEvent("onLoad", Module.runInitializationFunctions);
		}
		else{
			window.onLoad = Module.runInitializationFunctions();
		}
	}
	Module._registerEventHandler = function(){};
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值