下面的tab中把testX.htm加载过来
那么在各个testX.htm中的[color=blue]是否可以定义重名的全局变量,或者声明一样的函数[/color]呢?
我试了一下,如果这样的话,后打开的页面的全局变量会覆盖先打开的页面的变量
而后打开的页面的函数会覆盖先打开的页面的函数
那么[color=blue]我怎么阻止这种覆盖[/color]呢?
Ext.onReady(function(){
Ext.QuickTips.init();
var tabsDemo=new Ext.TabPanel({
renderTo:'hello',
activeTab:0,
width:500,
height:500,
enableTabScroll:true,
items:[]
});
var index=0;
Ext.get("AddNewTab").on("click",function(){
tabsDemo.add({
title:"newtab"+index,
id:"newtab"+index,
//html:"new tab"+index,
[color=darkred][b]autoLoad:{url:"test"+index+".htm",scripts:true},[/b][/color]
closable:true
});
tabsDemo.setActiveTab("newtab"+index);
index++;
})
});
[quote]能否举个简单的例子说一下“局部变量(命名空间)”呢?[/quote]
全局:
[code="java"]
function fun(){
}
//调用
fun();
[/code]
局部:
[code="java"]
var namespace = {};
namespace.fun=function(){
};
//调用
namespace.fun();
[/code]
1.是否可以定义重名的全局变量,或者声明一样的函数?
不能有同名的[b]全局[/b]函数或者[b]全局[/b]变量
2.我怎么阻止这种覆盖?
加载过来的函数或变量统一放在一个局部变量(命名空间)下面
testX.htm里面不需要导入EXT相关的js文件了,你只要写你想要的函数就好了。
testX.htm里面所有的js代码都放在一个命名空间下面,不会产生重名冲突