亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery源碼分析之jQuery中的循環(huán)技巧詳解

 更新時(shí)間:2014年09月06日 16:02:27   投稿:shichen2014  
這篇文章主要介紹了jQuery源碼中的循環(huán)技巧,包括各類選擇、判斷、遍歷等等操作,非常實(shí)用的技巧,需要的朋友可以參考下

jQuery的源碼中有很多值得學(xué)習(xí)借鑒的技巧,本文即收集了jQuery中出現(xiàn)的各種遍歷技巧和場(chǎng)景。具體分析如下:

// 簡(jiǎn)單的for-in(事件) 
for ( type in events ) { 
 
} 
// 緩存length屬性,避免每次都去查找length屬性,稍微提升遍歷速度 
// 但是如果遍歷HTMLCollection時(shí),性能提升非常明顯,因?yàn)槊看卧L問HTMLCollection的屬性,HTMLCollection都會(huì)內(nèi)部匹配一次所有的節(jié)點(diǎn) 
for ( var j = 0, l = handlers.length; j < l; j++ ) { 
 
} 
// 不比較下標(biāo),直接判斷元素是否為true(強(qiáng)制類型轉(zhuǎn)換) 
var elem; 
for ( var i = 0; elems[i]; i++ ) { 
  elem = elems[i]; 
  // ... 
} 
// 遍歷動(dòng)態(tài)數(shù)組(事件),不能緩存length屬性,j++之前先執(zhí)行j--,保證不會(huì)因?yàn)閿?shù)組下標(biāo)的錯(cuò)誤導(dǎo)致某些數(shù)組元素遍歷不到 
for ( j = 0; j < eventType.length; j++ ) { 
eventType.splice( j--, 1 ); 
} 
for ( var i = 1; i < results.length; i++ ) { 
  if ( results[i] === results[ i - 1 ] ) { 
    results.splice( i--, 1 ); 
  } 
} 
// 迭代過程中盡可能減少遍歷次數(shù)(事件),如果你能知道從哪里開始遍歷的話,這里是pos 
for ( j = pos || 0; j < eventType.length; j++ ) { 
 
} 
//倒序遍歷(事件),減少了幾個(gè)字符:循環(huán)條件判斷,合并i自減和i取值,倒序遍歷會(huì)有瀏覽器優(yōu)化,稍微提升遍歷速度 
for ( var i = this.props.length, prop; i; ) { 
  prop = this.props[ --i ]; 
  event[ prop ] = originalEvent[ prop ]; 
} 
// 倒序遍歷,中規(guī)中矩,倒序會(huì)有瀏覽器優(yōu)化,稍微提升遍歷速度 
for ( j = tbody.length - 1; j >= 0 ; --j ) { 
  if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) { 
    tbody[ j ].parentNode.removeChild( tbody[ j ] ); 
  } 
} 
//不判斷下標(biāo),直接判斷元素(選擇器) 
for ( i = 0; checkSet[i] != null; i++ ) { 
  if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && Sizzle.contains(context, checkSet[i])) ) { 
    results.push( set[i] ); 
  } 
} 
for ( ; array[i]; i++ ) { 
  ret.push( array[i] ); 
} 
// 不判斷下標(biāo),取出元素然后判斷元素(選擇器) 
for ( var i = 0; (item = curLoop[i]) != null; i++ ) { 
 
} 
// 遍歷DOM子元素 
for ( node = parent.firstChild; node; node = node.nextSibling ) { 
  if ( node.nodeType === 1 ) { 
    node.nodeIndex = ++count; 
  } 
} 
// 動(dòng)態(tài)遍歷DOM子元素(DOM遍歷),dir參數(shù)表示元素的方向?qū)傩裕鏿arentNode、nextSibling、previousSibling、lastChild和firstChild 
for ( ; cur; cur = cur[dir] ) { 
  if ( cur.nodeType === 1 && ++num === result ) { 
    break; 
  } 
} 
// while檢查下標(biāo)i 
var i = promiseMethods.length; 
while( i-- ) { 
  obj[ promiseMethods[i] ] = deferred[ promiseMethods[i] ]; 
} 
// while檢查元素 
while( (type = types[ i++ ]) ) { 
 
} 
// while遍歷動(dòng)態(tài)數(shù)組(AJAX),總是獲取第一個(gè)元素,檢查是否與特殊值相等,如果相等就從數(shù)組頭部移除,直到遇到不相等的元素或數(shù)組為空 
while( dataTypes[ 0 ] === "*" ) { 
  dataTypes.shift(); 
  if ( ct === undefined ) { 
    ct = s.mimeType || jqXHR.getResponseHeader( "content-type" ); 
  } 
} 
// while遍歷動(dòng)態(tài)數(shù)組(異步隊(duì)列),總是獲取第一個(gè)元素,直到數(shù)組為空,或遇到值為undefined的元素 
while( callbacks[ 0 ] ) { 
  callbacks.shift().apply( context, args ); 
} 
// while反復(fù)調(diào)用RegExp.exec(AJAX),能夠否反復(fù)調(diào)是exec比re.test、String.match更加強(qiáng)大的原因,每次調(diào)用都將lastIndex屬性設(shè)置到緊接著匹配字符串的字符位置 
while( ( match = rheaders.exec( responseHeadersString ) ) ) { 
  responseHeaders[ match[1].toLowerCase() ] = match[ 2 ]; // 將響應(yīng)頭以key-value的方式存在responseHeaders中 
}

希望本文所述對(duì)大家jQuery的WEB程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論