The Jquery.each method is used to traverse an array or object, and to process the currently traversed elements, which are used in jquery at a very high frequency, and are explained in detail below:
/*! * jquery Source Analysis-each function * jquery version: 1.4.2 * *----------------------------------------------------------* Function Introduction * * Each letter
Number is appended to the JQuery object via the Jquery.extend function: * Jquery.extend ({* Each:function () {} *}); * If you do not understand the Jquery.extend function source code, you can refer to the JQuery source analysis-extend function of the article * * Jquery.each method used to traverse an array or object, and the current traversal of the elements to deal with * The Jquery.each method can add an accompanying parameter to the handler (not exactly the same as the callback using the parameter without parameters) * *----------------------------------------------------------* Use instructions * Each function is not exactly the same as the type of the parameter: * 1, traversing the object (with additional parameters) * $.each (object, Function (P1, p2) {* This;//This is the current genus of object in each traversal Sex value * p1; P2;
Access additional Parameters *}, [' Parameter 1 ', ' parameter 2 ']); * * 2, traversal array (with attachment parameters) * $.each (array, function (P1, p2) {* This;//here is the current element * P1 p2;//Access additional parameters *}, [' Parameter 1 '] for each traversal array
, ' parameter 2 ']); * * 3, traversing the object (no additional parameters) * $.each (object, function (name, value) {* This;//this point to the current property's value * name;//name represents the name of the current attribute of object * VA Lue
Value represents the values of the current property of Object *}); * 4, traversal array (no additional parameters) * $.each (array, function (I, value) {* This;//this point to current element * I;//i represents array current subscript * value;
Value represents the current element of array *}); *----------------------------------------------------------*///jquery.each (), $.each ()//@param {object}| {Array} object needs to traverse the processed object or array//@param {function} callback traversal processing callback function//@param {Array} args callback callback function additional parameters Each:functio N (object, callback, args) {//When an object needs to be traversed, the name variable is used to record the object's property name Var name,///When an array needs to be traversed, the I variable is used to record the loop's array subscript i = 0,///traverse the array length, when required The object to be traversed is an array of storage array length//If you need to traverse an object, then length = = = Undefined length = object.length,//Check whether the 1th parameter object is an object//according to Object.leng Th excludes array types, excluding function types according to Isfunction (because functions are also objects) Isobj = length = = Undefined | |
Jquery.isfunction (object); When the callback function has additional parameters, it executes the first branch//if (!! args {if (args) {///need to traverse an object if (isobj) {//Traverse object property, name is the object's property name, then the top of the function has been declared//Many people are not accustomed to the for (var name in object) mode, if not sound
Ming, then name is defined as the global variable for (name in object) {//Call callback callback function, and the scope of the callback function is represented as the value of the current property///such as: Callback () {this;//) to the current property value Args the 3rd parameter of each as an additional parameter to the callback function if (callback.apply (object[name), args) = = False) {//If you use return False in the callback callback function;Perform the next cycle break; }}///need to traverse an array else {///loop length, loop variable I defined at top of function////loop variable self-increment within loop execute for (; i < length;) {//Call the callback function, consistent with the callback call of the above annotation//Here the This in the callback function points to the current array element if (callback.apply (object[i++), args) = False) {BR
Eak }}///The callback function does not have an additional parameter, executing the second branch else {//need to traverse is an object if (isobj) {//Loop object's property name, name at the top of the function is defined for (name in object) {// Call the callback callback function//In the traversal of an object without parameters, the scope is represented by the value of the current property//And the callback function contains two arguments, the first number is the current property name, and the second is the current property value//I think it would be better if the code were modified: Callback.call ( object, name, object[name] = = False) {if (Callback.call (object[name), name, object[name]) = = False) {//If callback back
Use return False in the function, the next loop break is not executed;
An array else {//need to traverse is a bit BT, interpreted as://var value = object[0]; for (; i < length;)
{//if (false = = = Callback.call (value, I, value)) {//break;
}//value = Object[++i]; In the same way, I think it would be better if the code here is slightly modified://for (; i < length && false!== Callback.call (object, I, object[i++]);) {//} for (var value = object[0]; I < length;& Callback.call (value, I, value)!== false; Value = Object[++i] {}}}//This returns the object or array being traversed, but object has not been changed, so it is generally not assigned to $.each (), but if used in the way I modified in the annotation, and in the callback callback function, the
(that is, a reference to object) is modified//The object returned here is the modified objects or arrays return object; }