前端模块化

 1 <script>
 2         var MyModules = (function Manager() {
 3             var modules = {};
 4             function define(name, deps, impl) {
 5                 for (var i = 0; i < deps.length; i++) {
 6                     deps[i] = modules[deps[i]];
 7                 }
 8                 modules[name] = impl.apply(impl, deps);
 9             }
10             function get(name) {
11                 return modules[name];
12             }
13             return {
14                 define: define,
15                 get: get,
16             }
17         })();
18         MyModules.define("bar", [], function () {
19             function hello(who) {
20                 return "Let me introduce:" + who;
21             }
22             return {
23                 hello:hello
24             }
25         })
26         MyModules.define("foo", ["bar"], function () {
27             var hungry = "hippo";
28             function awasome() {
29                 console.log(bar.hello(hungry).toUpperCase());
30             }
31             return {
32                 awasome:awasome,
33             }
34         });
35         var bar = MyModules.get("bar");
36         var foo = MyModules.get("foo");
37         console.log(bar.hello("sunnie"));
38         foo.awasome();
39     </script>

模块化

posted on 2017-01-10 09:47  sunnie_c  阅读(105)  评论(0编辑  收藏  举报