Home >Web Front-end >JS Tutorial >jQuery1.6 usage method 2_jquery

jQuery1.6 usage method 2_jquery

WBOY
WBOYOriginal
2016-05-16 17:59:03986browse

makeArray: function(array, results) {//Convert an array-like object into a real JavaScript array. results is an optional parameter
var ret = results || [];//results is used as an array to store arry. If it is not defined, it is set to an empty array
 if (array != null) {{//window, The .length of String, Function, and Array types is not undefined; (document.getElementById is undefined in IE, and jQuery.type (document.getElementById) is object, so methods like this are special under IE, and you can use the type method in jquery)
   var type = jQuery.type( array );
if ( array.length == null || type === "string" || type === "function" || type === "regexp " || jQuery.isWindow( array ) )
push.call( ret, array );// The object passed in is not an array, so push it and add it to the ret array
} else {
jQuery.merge( ret, array );//The incoming object is an array or similar array, directly merged into the ret array
}
}
return ret;
},
inArray: function( elem, array ) {//Search for the specified value in the array and return its index (returns -1 if not found).
if ( indexOf ) {//ECMA-262 standard browsers support it, and the IE series does not support Array.prototype.indexOf until IE9,
return indexOf.call( array, elem );
}
for ( var i = 0, length = array.length; i if ( array[ i ] === elem ) {
return i;
}
}
return -1;
},
merge: function( first, second) {//Merge arrays
var i = first.length ,
j = 0;
if ( typeof second.length === "number" ) {//Simply detect whether the object has a length attribute, and if so, add it to the end of the operation array in a loop
for ( var l = second.length; j first[ i ] = second[ j ];
}
} else {
while ( second[j] !== undefined ) {
first[ i ] = second[ j ];
}
}
first.length = i;
return first;
},
grep: function(elems, callback, inv) {//Find array elements that satisfy the filtering function. The original array is unaffected.
var ret = [], retVal;
inv = !!inv;//Convert to Boolean value type, if inv is not explicitly specified or specified as false, inv=false;
for ( var i = 0, length = elems.length; i retVal = !!callback( elems[ i ], i );//Convert to Boolean value type
if ( inv !== retVal ) {//If the result returned by callback is opposite to inv, it is retained
ret.push( elems[ i ] );
}
}
return ret;
},

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn