JavaScript 命名空间

 1 <script type="text/javascript">
 2 Namespace=new Object();
 3 Namespace.register=function(fullNS){
 4     var nsArray=fullNS.split('.');
 5     var sEval="";
 6     var sNS="";
 7     for(var i=0;i<nsArray.length;i++){
 8         if(i!=0){
 9             sNS+=".";
10         }
11         sNS+=nsArray[i];
12         sEval+="if(typeof("+sNS+")=='undefined')"+sNS+" = new Object();";
13     }
14     if(sEval!=""){
15         eval(sEval);
16     }
17 }
18 Namespace.register("Grandsoft.GEA");
19 Grandsoft.GEA.Person=function(name,age){
20     this.name=name;
21     this.age=age;
22 };
23 Grandsoft.GEA.Person.prototype.show=function(){
24     alert(this.name+"is"+this.age+"years old!");
25 }
26 var p=new Grandsoft.GEA.Person("yanglf",25);
27 p.show();
28 </script>
原文地址:https://www.cnblogs.com/lvyongbo/p/4497429.html