最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

// 簡單的for-in(事件) 
for ( type in events ) { 
 
} 
// 緩存length屬性,避免每次都去查找length屬性,稍微提升遍歷速度 
// 但是如果遍歷HTMLCollection時,性能提升非常明顯,因?yàn)槊看卧L問HTMLCollection的屬性,HTMLCollection都會內(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]; 
  // ... 
} 
// 遍歷動態(tài)數(shù)組(事件),不能緩存length屬性,j++之前先執(zhí)行j--,保證不會因?yàn)閿?shù)組下標(biāo)的錯誤導(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++ ) { 
 
} 
//倒序遍歷(事件),減少了幾個字符:循環(huán)條件判斷,合并i自減和i取值,倒序遍歷會有瀏覽器優(yōu)化,稍微提升遍歷速度 
for ( var i = this.props.length, prop; i; ) { 
  prop = this.props[ --i ]; 
  event[ prop ] = originalEvent[ prop ]; 
} 
// 倒序遍歷,中規(guī)中矩,倒序會有瀏覽器優(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; 
  } 
} 
// 動態(tài)遍歷DOM子元素(DOM遍歷),dir參數(shù)表示元素的方向?qū)傩?,如parentNode、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遍歷動態(tài)數(shù)組(AJAX),總是獲取第一個元素,檢查是否與特殊值相等,如果相等就從數(shù)組頭部移除,直到遇到不相等的元素或數(shù)組為空 
while( dataTypes[ 0 ] === "*" ) { 
  dataTypes.shift(); 
  if ( ct === undefined ) { 
    ct = s.mimeType || jqXHR.getResponseHeader( "content-type" ); 
  } 
} 
// while遍歷動態(tài)數(shù)組(異步隊(duì)列),總是獲取第一個元素,直到數(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中 
}

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

相關(guān)文章

最新評論

娱乐| 天祝| 吉安县| 新建县| 正宁县| 台前县| 通榆县| 金溪县| 自贡市| 南丹县| 永吉县| 云龙县| 司法| 石景山区| 年辖:市辖区| 福州市| 三门峡市| 芒康县| 西贡区| 阿瓦提县| 介休市| 石家庄市| 郎溪县| 衢州市| 冕宁县| 余江县| 乌兰察布市| 秦皇岛市| 义马市| 新龙县| 安泽县| 达州市| 田阳县| 朝阳区| 卓资县| 宣武区| 仙桃市| 兴国县| 和顺县| 云阳县| 元朗区|