Javascript 类、命名空间、代码组织

$(function () {
pageJs.urls
= [
'/', Index,
];
pageJs.run();
});

//the functions include html elements. Ajax function also can be put here.
var Common = {
init:
function () {
//alert('Common');
}
};

//url--pages
var Index = {
init:
function () {
Common.init();
this.test();
this.test2();
 
//alert(location.pathname);
    },
test:
function () {
 
//alert('test');
    }
};

Index.test2
=function () {
Index.test();
 
//alert('test2');
}

//should be remove to other js file, and load before this file.
//
the functions don't include any html elements.
var Utils = {
debug:
function () {
}
};

var pageJs = {
urls: [],
run:
function () {
var urls =this.urls;
if ((urls.length %2) !=0) {
throw"urls error";
}
var len = urls.length /2;
for (var i =0; i <= len; i = i +2) {
var pattern =new RegExp(urls[i], 'i');
if (pattern.test(location.pathname)) {
urls[i
+1].init();
break;
}
}
}
};

 2013/7/12 注:

我想做的东西,目前有一个框架可以很好的实现,sea.js, http://seajs.org/docs/

 

posted @ 2011-07-30 16:12  高天蒲  阅读(504)  评论(0编辑  收藏  举报
[url=http://t.sina.com.cn/1084588625?s=6uyXnP][img]http://service.t.sina.com.cn/widget/qmd/1084588625/a57ab42b/1.png[/img][/url]