js的名字空间用例
<script>__namespace__({com:{akira:{test:
(function(){
function A()
{
A.prototype.a = function(){alert('A in a');}
}
function B()
{
B.prototype.a = function(){alert('A in b');}
}B.prototype = new A(),B.prototype.base = new A();
return {A:A,B:B};
})()
}}})
var a = new com.akira.test.A();
a.a();
try{a = new A();}catch(e){alert("Class A not found in __global__!");}
__using__(com.akira.test);
var b = new B();
b.a();
__unlink__(com.akira.test);
try{b = new B();}catch(e){alert("Class B not found in __global__!");}
with(com.akira.test)
{
var c = new B();
c.base.a();
}
/********************************************************
Javascript namespaces V1.00 author Akira
*********************************************************/
function __namespace__(ns,parent)
{
if(parent == null) parent = self;
for(var each in ns)
{
if(parent != null)
__namespace__(ns,parent);
else
parent = ns;
}
return;
}
function __using__(ns)
{
for(var each in ns)
if(self==null) self = ns;
}
function __unlink__(ns)
{
for(var each in ns)
{
if(self == ns) self = null;
}
}
</script>
页:
[1]