时间:2021-07-17 08:10:58 | 栏目:JavaScript代码 | 点击:次
定义
var MyModules = (function Manager() { var modules = {}; function define (name, deps, impl) { for(var j = 0, length = deps.length; j < length; j++){ deps[j] = modules[deps[j]]; } modules[name] = impl.apply(impl, deps); } function get (name) { return modules[name]; } return { define: define, get: get } })();
使用
MyModules.define('test1', [], function() { function hello(name) { console.log(name); } return { hello: hello } }); MyModules.define('test2', ['test1'], function(test1) { function age(name, age) { console.log(test1.hello(name)); console.log(age); } return { age: age } }); MyModules.get('test2').age('mumu', '27');