一个简单的加载器

by 苹果熊

JavaScript

(function(win) {
	var mod = {}
	function define(name, deps, fn) {
		for(var i = 0; i < deps.length; i++) {
			deps[i] = mod[deps[i]]
		}
		mod[name] = fn.apply(fn, deps)
	}
	function get(name) {
		return mod[name]
	}
	return win.mymod = {define: define, get: get}
})(window)
mymod.define('a', [], function() {
	function hello(who) {
		return 'hello' + who
	}
	return {hello: hello}
})
mymod.define('b', ['a'], function(a) {
	function hi() {
		console.log(a.hello('pgx').toUpperCase())
	}
	return {hi: hi}
})

mymod.get('a').hello(' pgx')