文章目錄

传统的是这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var current = null;
var labels = {
'home':'home',
'articles':'articles',
'contact':'contact'
};
function init(){
};
function show(){
current = 1;
};
function hide(){
show();
};

对象的写法这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
demo = {
current:null,
labels:{
'home':'home',
'articles':'articles',
'contact':'contact'
},
init:function(){
},
show:function(){
demo.current = 1;
},
hide:function(){
demo.show();
}
}

模块化的是这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module = function(){
var labels = {
'home':'home',
'articles':'articles',
'contact':'contact'
};
return {
current:null,
init:function(){
},
show:function(){
module.current = 1;
},
hide:function(){
module.show();
}
}
}();

优化后是这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module = function(){
var current = null;
var labels = {
'home':'home',
'articles':'articles',
'contact':'contact'
};
var init = function(){
};
var show = function(){
current = 1;
};
var hide = function(){
show();
}
return{init:init, show:show, current:current}
}();
module.init();

国外的月亮是圆的

文章目錄