晴明的博客园 GitHub      CodePen      CodeWars     

[js] namespace(命名空间)

#

 1 <!DOCTYPE html>
 2 <html>
 3 
 4     <head>
 5         <meta charset="UTF-8">
 6         <title></title>
 7     </head>
 8 
 9     <body>
10         <script>
11             //原理。可能需要规定除了公共方法,或者明确变量名不冲突,其他函数统一用funciton(){}方式写
12             function a() {
13                 console.log("Me duplicate.");
14             }
15             var a = function() {
16                 console.log("Me original.");
17             }
18             a(); //Me original.
19             var b = function() {
20                 console.log("Me original.");
21             }
22 
23             function b() {
24                 console.log("Me duplicate.");
25             }
26             b(); //Me original.
27             var c = function() {
28                 console.log("Me duplicate.");
29             }
30             var c = function() {
31                 console.log("Me original.");
32             }
33             c(); //Me original.
34             var d;
35             d = function() {
36                 console.log("Me duplicate.");
37             }
38             d = function() {
39                 console.log("Me original.");
40             }
41             d(); //Me original.
42             //例子,5种写法
43             var NameSpace = window.NameSpace || {};
44             NameSpace.Hello = function() {
45                 this.name = 'world';
46             };
47             NameSpace.Hello.prototype.sayHello = function(_name) {
48                 return 'Hello ' + (_name || this.name);
49             };
50             var hello = new NameSpace.Hello();
51             console.log(hello.sayHello());
52             hello.name = 'earth';
53             console.log(hello.sayHello());
54             //console.log(window.NameSpace);
55             var NameSpace2 = window.NameSpace || {};
56             NameSpace2.Hello = {
57                 name: 'moon',
58                 sayHello: function(_name) {
59                     return 'Hello ' + (_name || this.name);
60                 }
61             };
62             //console.log(window.NameSpace);
63             console.log(hello.sayHello());
64             console.log(NameSpace2.Hello.sayHello());
65             var NameSpace3 = window.NameSpace || {};
66             NameSpace3.Hello = (function() {
67                 var self = {};
68                 var name = 'Venus';
69                 self.sayHello = function(_name) {
70                     return 'Hello ' + (_name || name);
71                 };
72                 return self;
73             }());
74             console.log(NameSpace3.Hello.sayHello());
75             var NameSpace4 = window.NameSpace || {};
76             NameSpace4.Hello = (function() {
77                 var name = 'Mars';
78                 var sayHello = function(_name) {
79                     return 'Hello ' + (_name || name);
80                 };
81                 return {
82                     sayHello: sayHello
83                 };
84             }());
85             console.log(NameSpace4.Hello.sayHello());
86             var NameSpace5 = window.NameSpace || {};
87             NameSpace5.Hello = new function() {
88                 var self = this; //防止指向变化
89                 var name = 'Jupiter';
90                 self.sayHello = function(_name) {
91                     return 'Hello ' + (_name || name);
92                 };
93             };
94             console.log(NameSpace5.Hello.sayHello());
95         </script>
96     </body>
97 
98 </html>

 

posted @ 2016-02-21 16:13  晴明桑  阅读(286)  评论(0编辑  收藏  举报