var mymodules = (function Manager () {
var modules={};
Variable an array group callback function
function define (name, Deps, Impl) {
for (var i=0; i<deps.length; i++) {
Deps[i] = modules[deps[i]];
}
Modules[name] = impl.apply (Impl, deps);
Apply the first parameter and the caller, you can directly pass NULL = = Modules[name] = impl.apply (null, deps);
}
function get (name) {
return Modules[name];
}
return {
Define:define,//Setting the value of modules
Get:get//Get the value of modules
};
})();
Mymodules.define ("Bar", [], function () {
function Hello (WHO) {
Return ' Let me introduce: ' + who;
}
return {
Hello:hello
};
} );
After execution, the value of modules is changed as follows:
Modules = {
"Bar": {
"Hello": function Hello (WHO) {
Return ' Let me introduce: ' + who;
// }
// }
// }
Mymodules.define ("foo", ["bar"], function (bar) {
var hungry = "Hippo";
function Awesome () {
Console.log (Bar.hello (Hungry). toUpperCase ());
}
return {
Awesome:awesome
};
} );
After execution, the value of modules is changed as follows:
Modules = {
"Foo": {
"Awesome": function () {
Console.log ({"Hello": function Hello (WHO) {return ' Let me introduce: ' + Who;}}. Hello (Hungry). toUpperCase ());
// }
// }
// }
var bar = mymodules.get ("bar");
var foo = mymodules.get ("foo");
Console.log (Bar.hello ("Hippo")); Let me Introduce:hippo
Foo.awesome (); Let ME Introduce:hippo
JS Module function