一個瀏覽器端加載基於AMD加載JS的實現

未測試:瀏覽器

AMD:

define, require

const modules = [];

loadScript = function(script, cb){
	// 瀏覽器加載script
	
	script.onLoad = function(){

	}
}

load = function(module, cb){
	loadScript(module.path, cb);
}

loadModules = function(ids, cb){
	var done = function(){
		if(ids.every( id => cache[id].load)){
			cb.apply({}, ids.map(id => cache[id].fn));
		}
	};

	ids.map(id => {
		if(!cache[id].load){
			load(cache[id], done)
		}
	})
}

define = function(id, deps, fn){
	cache[id] = {
		load: true,
		deps: deps,
		fn: fn,
	}
}

require = function(deps, fn){
	loadModules(deps, function(modules){
		fn.apply({}, modules);
	})
}
相關文章
相關標籤/搜索