<html>
<body>
<script type="text/javascript">
(function(){
function showAlert()
{
alert("this is a inner function");
}
window["namespace"] = {};
window["namespace"]["showFun"] = showAlert;
})()
namespace.showFun();
</script>
</body>
</html>
使用伪命名空间可以很好的保护自己的属性,变量和方法。而且,由于他们都存在与同一个函数之中,所以他们之间任然可以互相访问。
补充:
/************* 键值对 object*******************/
var MyNamePace = {
"Init":function(){
alert("Init函数");
},
"Load":function(){
alert("Load函数");
}
}
MyNamePace.Init();
MyNamePace["Load"]();
/************* 键值对 object *******************/
/************* 伪类 object *******************/
function MyFun()
{
this.showMsg = function(){
alert("公共方法");
}
var InnerMsg = function(){
alert("私有方法");
}
}
var ele = new MyFun();
ele.showMsg();
//ele.InnerMsg(); 没办法进行调用
/************* 伪类 object *******************/