js模块化认识1

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8 <script>
 9 var module = {
10     _count:0,
11     m1:function(){
12        var c
13     },
14     m2:function(){
15         return _count--;
16     }
17 };
18     module.m1();
19     console.log(module._count);
20 
21 //    var module1 = (function(){
22 //        var _count = 0;
23 //        var m1 = function(){
24 //            _count++;
25 //            return _count;
26 //        };
27 //        var m2 = function(){
28 //            _count--;
29 //        }
30 //        return {
31 //            m1:m1,
32 //            m2:m2
33 //        }
34 //    })();
35 //    console.log(module1.m1());
36 
37 //1、下面这种方法的问题在于如果module1没有载入就会报错;
38 //    var module2 = (function(mod){
39 //        mod.m3 = function(){
40 //          alert("我是魔都乐")
41 //        };
42 //        return mod;
43 //    })(module1);
44 //2、下面的办法就解决了上面的问题传入 window.module1 和 {};
45     var module2 = (function(mod){
46         mod.m3 = function(){
47           alert("我是魔都乐")
48         };
49         return mod;
50     })(window.module1 || {});
51 
52     module2.m3();
53 
54 </script>
55 </body>
56 </html>

http://www.ruanyifeng.com/blog/2012/10/javascript_module.html

坚持下去就能成功
原文地址:https://www.cnblogs.com/suoking/p/4929501.html