创建命名空间 a.b.c.d
也就是创建一个对象,其结构如下:
a{
b:{
c:{
d:{}
}
}
}
通过这种方式保证了对象方法的惟一性
var Namespace = {
register: function(fullNS){
var nsArray = fullNS.split('.');
var sEval = "";
var sNS = "";
var namespace = window;
for(var i=0,k=nsArray.length;i<k;i++){
if (i != 0) sNS += ".";
sNS += nsArray[i];
sEval += "if (typeof(" + sNS + ") == 'undefined') " + sNS + " = new Object();";
}
if (sEval != "") eval(sEval);
}
}
Namespace.register("Grandsoft.GEA");
Namespace.register("Grandsoft.GCM");
Grandsoft.GEA.Person = function(name, age)
{
this.name = name;
this.age = age;
}
// 给类Person添加一个公共方法show()
Grandsoft.GEA.Person.prototype.show = function()
{
alert(this.name + " is " + this.age + " years old!");
}
// 演示如何使用类Person
var p = new Grandsoft.GEA.Person("xss", 28);
p.show();