欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS完成代碼前最好對其做5件事

 更新時(shí)間:2013年04月07日 09:41:39   作者:  
我們不得面對這樣一個(gè)事實(shí):許多程序員不會規(guī)劃他們的JS代碼。我們經(jīng)??焖賹懲甏a、運(yùn)行、提交。但當(dāng)我們繼續(xù)開發(fā)遇到變量和函數(shù)時(shí)不得不再次回頭查看它們代表的含義,麻煩就從這里開始了。

寫在前面

我們不得面對這樣一個(gè)事實(shí):許多程序員不會規(guī)劃他們的JS代碼。我們經(jīng)??焖賹懲甏a、運(yùn)行、提交。但當(dāng)我們繼續(xù)開發(fā)遇到變量和函數(shù)時(shí)不得不再次回頭查看它們代表的含義,麻煩就從這里開始了。同樣當(dāng)我們在其他程序員手中獲取腳本也會遇到類似的錯(cuò)誤。因此,當(dāng)我們說”this is done, I can go on”時(shí)最好對腳本做下列5件事情。

問題描述

現(xiàn)在我們想給每一個(gè)帶有class屬性為collapsible的DIV內(nèi)部添加超鏈接A,來顯示和隱藏DIV。

下面是用模塊函數(shù)編寫的實(shí)現(xiàn)代碼:

復(fù)制代碼 代碼如下:

var collapser = (function(){
var secs = document.getElementsByTagName('div');
for(var i=0;i<secs.length;i++){
if(secs[i].className.indexOf('collapsible')!==-1){
var p = document.createElement('p');
var a = document.createElement('a');
a.setAttribute('href','#');
a.onclick = function(){
var sec = this.parentNode.nextSibling;
if(sec.style.display === 'none'){
sec.style.display = 'block';
this.firstChild.nodeValue = 'collapse'
} else {
sec.style.display = 'none';
this.firstChild.nodeValue = 'expand'
}
return false;
};
a.appendChild(document.createTextNode('expand'));
p.appendChild(a);
secs[i].style.display = 'none';
secs[i].parentNode.insertBefore(p,secs[i]);
}
}
})();

上面的代碼已經(jīng)很準(zhǔn)確的實(shí)現(xiàn)了我們想要的結(jié)果。但是我們還可以對上面的代碼進(jìn)一步的重構(gòu)。

第一步:樣式(CSS)與行為(JavaScript)分離

我們可以用添加一個(gè)CSS的class選擇器來消除通過JS中設(shè)置的樣式。這種現(xiàn)象在新手中經(jīng)常遇到.

復(fù)制代碼 代碼如下:

var collapser = (function(){
var secs = document.getElementsByTagName('div');
for(var i=0;i<secs.length;i++){
if(secs[i].className.indexOf('collapsible')!==-1){
secs[i].className += ' ' + 'collapsed';
var p = document.createElement('p');
var a = document.createElement('a');
a.setAttribute('href','#');
a.onclick = function(){
var sec = this.parentNode.nextSibling;
if(sec.className.indexOf('collapsed')!==-1){
sec.className = sec.className.replace(' collapsed','');
this.firstChild.nodeValue = 'collapse'
} else {
sec.className += ' ' + 'collapsed';
this.firstChild.nodeValue = 'expand'
}
return false;
}
a.appendChild(document.createTextNode('expand'));
p.appendChild(a);
secs[i].parentNode.insertBefore(p,secs[i]);
}
}
})();

第二步:對代碼進(jìn)一步性能優(yōu)化

這里我們可以做兩件事情:1、循環(huán)語句中secs的length屬性可以用變量保存。2、為事件處理器創(chuàng)建重用的函數(shù)。好處是減少事件處理器數(shù)量,減少內(nèi)存占用。

復(fù)制代碼 代碼如下:

var collapser = (function(){
var secs = document.getElementsByTagName('div');
for(var i=0,j=secs.length;i<j;i++){
if(secs[i].className.indexOf('collapsible')!==-1){
secs[i].className += ' ' + 'collapsed';
var p = document.createElement('p');
var a = document.createElement('a');
a.setAttribute('href','#');
a.onclick = toggle;
a.appendChild(document.createTextNode('expand'));
p.appendChild(a);
secs[i].parentNode.insertBefore(p,secs[i]);
}
}
function toggle(){
var sec = this.parentNode.nextSibling;
if(sec.className.indexOf('collapsed')!==-1){
sec.className = sec.className.replace(' collapsed','');
this.firstChild.nodeValue = 'collapse'
} else {
sec.className += ' ' + 'collapsed';
this.firstChild.nodeValue = 'expand'
}
return false;
}
})();

第三步:添加配置對象

使用配置對象存放代碼中的硬編碼,如使用到的文本標(biāo)簽或自定義的屬性名。有利于后續(xù)的維護(hù)。

復(fù)制代碼 代碼如下:

var collapser = (function(){
var config = {
indicatorClass : 'collapsible',
collapsedClass : 'collapsed',
collapseLabel : 'collapse',
expandLabel : 'expand'
}
var secs = document.getElementsByTagName('div');
for(var i=0,j=secs.length;i<j;i++){
if(secs[i].className.indexOf(config.indicatorClass)!==-1){
secs[i].className += ' ' + config.collapsedClass;
var p = document.createElement('p');
var a = document.createElement('a');
a.setAttribute('href','#');
a.onclick = toggle;
a.appendChild(document.createTextNode(config.expandLabel));
p.appendChild(a);
secs[i].parentNode.insertBefore(p,secs[i]);
}
}
function toggle(){
var sec = this.parentNode.nextSibling;
if(sec.className.indexOf(config.collapsedClass)!==-1){
sec.className = sec.className.replace(' ' + config.collapsedClass,'');
this.firstChild.nodeValue = config.collapseLabel
} else {
sec.className += ' ' + config.collapsedClass;
this.firstChild.nodeValue = config.expandLabel
}
return false;
}
})();

第四步:為變量和函數(shù)起有含義的名字

復(fù)制代碼 代碼如下:

var collapser = (function(){
var config = {
indicatorClass : 'collapsible',
collapsedClass : 'collapsed',
collapseLabel : 'collapse',
expandLabel : 'expand'
}
var sections = document.getElementsByTagName('div');
for(var i=0,j=sections.length;i<j;i++){
if(sections[i].className.indexOf(config.indicatorClass) !== -1){
sections[i].className += ' ' + config.collapsedClass;
var paragraph = document.createElement('p');
var trigger = document.createElement('a');
trigger.setAttribute('href','#');
trigger.onclick = toggleSection;
trigger.appendChild(document.createTextNode(config.expandLabel));
paragraph.appendChild(trigger);
sections[i].parentNode.insertBefore(paragraph,sections[i]);
}
}
function toggleSection(){
var section = this.parentNode.nextSibling;
if(section.className.indexOf(config.collapsedClass) !== -1){
section.className = section.className.replace(' ' + config.collapsedClass,'');
this.firstChild.nodeValue = config.collapseLabel
} else {
section.className += ' ' + config.collapsedClass;
this.firstChild.nodeValue = config.expandLabel
}
return false;
}
})();

第五步:添加必要的注釋

復(fù)制代碼 代碼如下:

// Collapse and expand section of the page with a certain class
// written by Christian Heilmann, 07/01/08
var collapser = (function(){
// Configuration, change CSS class names and labels here
var config = {
indicatorClass : 'collapsible',
collapsedClass : 'collapsed',
collapseLabel : 'collapse',
expandLabel : 'expand'
}
var sections = document.getElementsByTagName('div');
for(var i=0,j=sections.length;i<j;i++){
if(sections[i].className.indexOf(config.indicatorClass) !== -1){
sections[i].className += ' ' + config.collapsedClass;
var paragraph = document.createElement('p');
var trigger = document.createElement('a');
trigger.setAttribute('href','#');
trigger.onclick = toggleSection;
trigger.appendChild(document.createTextNode(config.expandLabel));
paragraph.appendChild(trigger);
sections[i].parentNode.insertBefore(paragraph,sections[i]);
}
}
function toggleSection(){
var section = this.parentNode.nextSibling;
if(section.className.indexOf(config.collapsedClass) !== -1){
section.className = section.className.replace(' ' + config.collapsedClass,'');
this.firstChild.nodeValue = config.collapseLabel
} else {
section.className += ' ' + config.collapsedClass;
this.firstChild.nodeValue = config.expandLabel
}
return false;
}
})();

 

相關(guān)文章

最新評論