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

javascript兩段代碼,兩個(gè)小技巧

 更新時(shí)間:2010年02月04日 10:08:44   作者:  
話說(shuō)以前我轉(zhuǎn)過(guò)一片文章,講的是 JavaScript中||和&&的妙用, 下面是一些優(yōu)化寫(xiě)法。
第一段代碼就是強(qiáng)調(diào)一下這個(gè)用法,我在我的項(xiàng)目中使用了一個(gè)switch,后來(lái)我發(fā)現(xiàn)這樣的代碼好丑,于是我就寫(xiě)成||&&形式的, 后來(lái)測(cè)試性能的時(shí)候,發(fā)現(xiàn)性能竟然上了一個(gè)數(shù)量級(jí),可見(jiàn)這種寫(xiě)法在某些情況下可以增加性能,但是我并不確定是何種情況才能提高性能,因?yàn)槲覝y(cè)試在通常情況下switch和||&&的性能是差不多的.
原來(lái)的代碼:
復(fù)制代碼 代碼如下:

switch(this.now_char=this.str.charAt(this.index)){
case "/":
if(this.handleNote()) continue;else this.str2+=this.now_char;
break;
case "\"":
case "\'":
if(this.handleStr()) continue;else this.str2+=this.now_char;
break;
case "\n":
if(this.handleLine()) continue;else this.str2+=this.now_char;
break;
case "{":
case "}":
if(this.handleDepth()) continue;else this.str2+=this.now_char;
break;
case ":":if(this.handleJson()) continue;else this.str2+=this.now_char;break;
default:
if(this.handleKeyword()) continue;else this.str2+=this.now_char;
break;
}

改寫(xiě)后的代碼,功能當(dāng)然是一樣的 view sourceprint?1 (this.now_char=="/"&&(this.handleNote()||(this.str2+=this.now_char)))||
((this.now_char=="\""||this.now_char=="\'")&&(this.handleStr()||(this.str2+=this.now_char)))||
(this.now_char=="\n"&&(this.handleLine()||(this.str2+=this.now_char)))|| ((this.now_char=="{"||this.now_char=="}")&&(this.handleDepth()||(this.str2+=this.now_char)))||
(this.handleKeyword()||(this.str2+=this.now_char))
我嚼的第二種寫(xiě)法更簡(jiǎn)潔點(diǎn),||&&還有很多用處,可以看那篇文章的介紹
第二段代碼是利用了一個(gè)特性: (ele=document.createElement("div")) ;//這個(gè)表達(dá)式會(huì)返回一個(gè)dom元素,賦值的同時(shí)會(huì)把值返回給外邊的括號(hào)
于是出來(lái)下面這段代碼 :
復(fù)制代碼 代碼如下:

var mixin=function(target,options){
for(var i in options){
target[i]=options[i]
}
}
var ele=null;
mixin(ele=document.createElement("div"),{
id:"aa",
className:"bb",
innerHTML:"sss"
})
document.body.appendChild(ele)
debug(ele.id)//aa
debug(ele.className)//bb
debug(ele.innerHTML)//sss

這段代碼是因?yàn)槲覍?shí)在厭煩了建立一個(gè)dom元素的時(shí)候的一大堆語(yǔ)句:
復(fù)制代碼 代碼如下:

var ele=document.createElement("div")
ele.id="aa";
ele.className="aa"
ele.innerHTML="sss"

等等等等,好煩啊.
于是出來(lái)了上面的代碼.
用上面的原理還可以這樣寫(xiě)代碼 (ele=document.createElement("div")).className="aa"; 感覺(jué)是不是節(jié)省了一點(diǎn)空間呢,上面這句話節(jié)省了一個(gè)變量名,呵呵.

相關(guān)文章

最新評(píng)論