Ext2.0中,Ext类有个namespace方法,该方法的作用是把传入的参数转换成对象.使用该方法的目的主要在于可以区分类名相同的类,这有点和java中的package作用类似.让我们先看下源码:

namespace : function()
{
var a=arguments, o=null, i, j, d, rt;

for (i=0; i<a.length; ++i)
{
d=a[i].split(".");
rt = d[0];
eval('if (typeof ' + rt + ' == "undefined")
{' + rt + ' =
{};} o = ' + rt + ';');

for (j=1; j<d.length; ++j)
{

o[d[j]]=o[d[j]] ||
{};
o=o[d[j]];
}
}
}
从代码可以看出,如果我们传入的字符串参数是以"."分割的,那么将会创建多个对象,比如:
Ext.namespace('system.corp');
则会创建2个对象,相当于执行了下面的代码:

system =
{};

system.corp =
{};
这样,我们在自定义类的时候就能这样使用:
Ext.namespace('system.corp');


system.corp.ManageCorp = function()
{
//dosomething
}
如果还想定义一个同名的类,那么可以就使用不同的namespace来区分,这样2个类就不会冲突了:
Ext.namespace('system.admin');


system.admin.ManageCorp = function()
{
//dosomething
}
此外,注意源码中"eval"方法的使用,如果有需要可以采用这种方式来解决问题.
posted on 2008-03-19 22:24
zhangchao 阅读(1660)
评论(8) 编辑 收藏 所属分类:
Ext