jQuery1.6 使用方法二

來源:互聯網
上載者:User

makeArray: function( array, results ) {//轉換一個類似數組的對象成為真正的JavaScript數組。 results為選擇性參數
var ret = results || [];//results作為存放arry的數組,如果沒有定義就設定為空白數組
         if ( array != null ) {{//window,String,Function,Array類型的.length不為undefined;(document.getElementById IE 為undefined,jQuery.type(document.getElementById )為object,所以類似這樣的方法在IE下比較特別,可以jquery裡的type方法)
        var type = jQuery.type( array );
if ( array.length == null || type === "string" || type === "function" || type === "regexp" || jQuery.isWindow( array ) )
push.call( ret, array );// 傳入的對象不是數組,就push加入ret數組中
} else {
jQuery.merge( ret, array );//傳入對象是數組或者類似數組,直接合并到ret數組中
}
}
return ret;
},
inArray: function( elem, array ) {//搜尋數組中指定值並返回它的索引(如果沒有找到則返回-1)。
if ( indexOf ) {//ECMA - 262標準的瀏覽器都支援,IE系列要到IE9才支援Array.prototype.indexOf,
return indexOf.call( array, elem );
}
for ( var i = 0, length = array.length; i < length; i++ ) {//IE9以下版本,通過迴圈數組來判斷
if ( array[ i ] === elem ) {
return i;
}
}
return -1;
},
merge: function( first, second ) {//合并數組
var i = first.length,
j = 0;
if ( typeof second.length === "number" ) {//簡單檢測對象是否有length屬性,有則迴圈添加到運算元組後面
for ( var l = second.length; j < l; j++ ) {//String,Array類型
first[ i++ ] = second[ j ];
}
} else {
while ( second[j] !== undefined ) {
first[ i++ ] = second[ j++ ];
}
}
first.length = i;
return first;
},
grep: function( elems, callback, inv ) {//尋找滿足過濾功能數組元素。原始數組不受影響。
var ret = [], retVal;
inv = !!inv;//轉化成布爾實值型別,如果沒有明確指定inv或者指定為false,inv=false;
for ( var i = 0, length = elems.length; i < length; i++ ) {
retVal = !!callback( elems[ i ], i );//轉化成布爾實值型別
if ( inv !== retVal ) {//callback返回的結果如果與inv相反,即保留
ret.push( elems[ i ] );
}
}
return ret;
},

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.