javascript兩段代碼,兩個小技巧
更新時間:2010年02月04日 10:08:44 作者:
話說以前我轉過一片文章,講的是 JavaScript中||和&&的妙用, 下面是一些優(yōu)化寫法。
第一段代碼就是強調(diào)一下這個用法,我在我的項目中使用了一個switch,后來我發(fā)現(xiàn)這樣的代碼好丑,于是我就寫成||&&形式的, 后來測試性能的時候,發(fā)現(xiàn)性能竟然上了一個數(shù)量級,可見這種寫法在某些情況下可以增加性能,但是我并不確定是何種情況才能提高性能,因為我測試在通常情況下switch和||&&的性能是差不多的.
原來的代碼:
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;
}
改寫后的代碼,功能當然是一樣的 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))
我嚼的第二種寫法更簡潔點,||&&還有很多用處,可以看那篇文章的介紹
第二段代碼是利用了一個特性: (ele=document.createElement("div")) ;//這個表達式會返回一個dom元素,賦值的同時會把值返回給外邊的括號
于是出來下面這段代碼 :
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
這段代碼是因為我實在厭煩了建立一個dom元素的時候的一大堆語句:
var ele=document.createElement("div")
ele.id="aa";
ele.className="aa"
ele.innerHTML="sss"
等等等等,好煩啊.
于是出來了上面的代碼.
用上面的原理還可以這樣寫代碼 (ele=document.createElement("div")).className="aa"; 感覺是不是節(jié)省了一點空間呢,上面這句話節(jié)省了一個變量名,呵呵.
原來的代碼:
復制代碼 代碼如下:
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;
}
改寫后的代碼,功能當然是一樣的 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))
我嚼的第二種寫法更簡潔點,||&&還有很多用處,可以看那篇文章的介紹
第二段代碼是利用了一個特性: (ele=document.createElement("div")) ;//這個表達式會返回一個dom元素,賦值的同時會把值返回給外邊的括號
于是出來下面這段代碼 :
復制代碼 代碼如下:
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
這段代碼是因為我實在厭煩了建立一個dom元素的時候的一大堆語句:
復制代碼 代碼如下:
var ele=document.createElement("div")
ele.id="aa";
ele.className="aa"
ele.innerHTML="sss"
等等等等,好煩啊.
于是出來了上面的代碼.
用上面的原理還可以這樣寫代碼 (ele=document.createElement("div")).className="aa"; 感覺是不是節(jié)省了一點空間呢,上面這句話節(jié)省了一個變量名,呵呵.
相關文章
js調(diào)用父框架函數(shù)與彈窗調(diào)用父頁面函數(shù)的簡單方法
下面小編就為大家?guī)硪黄猨s調(diào)用父框架函數(shù)與彈窗調(diào)用父頁面函數(shù)的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11