Created
November 9, 2016 07:29
-
-
Save wuliupo/a9c53641571d201961062cd13c37ecf6 to your computer and use it in GitHub Desktop.
JavaScript modern module definition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var MyModules = (function() { | |
| var modules = {}; | |
| return { | |
| defineModule(name, deps, impl) { | |
| for (var i = 0; i < deps.length; i++) { | |
| deps[i] = modules[deps[i]]; | |
| } | |
| modules[name] = impl.apply(impl, deps); | |
| }, getModule(name) { | |
| return modules[name]; | |
| } | |
| }; | |
| })(); | |
| MyModules.defineModule("bar", [], function () { | |
| return { | |
| hello(who) { | |
| return "Let me introduce: " + who; | |
| } | |
| }; | |
| }); | |
| MyModules.defineModule("foo", ["bar"], function (bar) { | |
| var hungry = "hippo"; | |
| return { | |
| awesome() { | |
| return bar.hello(hungry).toUpperCase(); | |
| } | |
| }; | |
| }); | |
| var bar = MyModules.getModule("bar"); | |
| var foo = MyModules.getModule("foo"); | |
| bar.hello("hippo"); // Let me introduce: hippo | |
| foo.awesome(); // LET ME INTRODUCE: HIPPO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment