This article is partially intercepted from the line and the thought
The Jquery.each method is used to iterate over an array or an object and to process the currently traversed element, which is used very often in jquery, which is explained in detail below:
Copy Code Code/*! * jquery Source Analysis-each function * jquery version: 1.4.2 * *----------------------------------------------------------* Function Introduction * * Each function is appended to the JQuery object via the Jquery.extend function: * Jquery.extend ({* Each:function () {}}); * If the Jquery.extend function source code still do not understand, you can refer to the "JQuery Source analysis-extend Function" Article * * * Jquery.each method is used to traverse an array or an object, and to process the currently traversed element * Jquery.each method can be considered The processing function increases the accompanying parameters (with parameters not exactly consistent with the callback using parameters) * *----------------------------------------------------------* Instructions for use * Each function is based on The effect of the type implementation of the parameter is not exactly the same: * 1, traversing the object (with additional parameters) * $.each (object, Function (P1, p2) {* this; Here the this points to the current property value of the object in each traversal * p1; P2; Access additional Parameters *}, [' Parameter 1 ', ' parameter 2 ']); * * 2, traversal array (with attachment parameters) * $.each (array, function (P1, p2) {* this; Here the this points to the current element of the array in each traversal * p1; P2; Access additional Parameters *}, [' Parameter 1 ', ' parameter 2 ']); * 3, Traverse object (no additional parameters) * $.each (object, function (name, value) {* this; This points to the value of the current property * name; Name denotes the current property of object * value; Value denotes the current property of the Object *}); * * 4, iterating over the array (noAdditional parameters) * $.each (Array, function (i, value) {* this; This points to the current element * I; I represents the array current subscript * value; Value represents the current element of the array *}); * ---------------------------------------------------------- **/ //Jquery.each (), $.each ()//@param {object}| {Array} object needs to traverse the processed objects or arrays//@param {Function} callback traversal processing callback function//additional parameters for @param {Array} args callback callback functionEach:function (Object, callback, args) { //When you need to traverse an object, the name variable is used to record the object's property name varname,//when you need to traverse an array, I variables are used to record the array subscript of the loopi =0, //iterates over the length of an array, storing the array length when the object to be traversed is an array//If you need to traverse an object, the length = = = UndefinedLength =Object. Length,//Check the 1th argument whether object is an object//Excludes the array type according to Object.length, excluding the function type according to Isfunction (because the function is also an object)Isobj = Length = = = Undefined | | Jquery.isfunction (Object); //when the callback function has additional parameters, the first branch is executed//if (!! args) { if(args) {//an object that needs to be traversed if(isobj) {//Traverse Object Properties, name is the property name of the object, and the top of the function is declared//Many people are not accustomed to the for-(var name in object) way, and if not declared, then name is defined as a global variable for(Nameinch Object) { //Call the callback callback function, and the scope of the callback function is expressed as the value of the current property//such as: Callback () {this;//This in the function points to the current property value//The 3rd parameter of each is args 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, the next loop is not executed Break; } } } //the one that needs to be traversed is an array Else { //loop length, loop variable i is defined at the top of the function//the self-increment of the loop variable executes inside the loop for(; I <length;) { //call the callback function, consistent with the callback invocation of the comment above//This in the callback function here points to the current array element if(Callback.apply (Object[i++], args) = = =false) { Break; } } } } //A second branch is executed when the callback function has no additional parameters Else { //an object that needs to be traversed if(isobj) {//The property name of the looping object, name defined at the top of the function for(Nameinch Object) { //Call the callback callback function//in an object traversal without parameters, the scope is represented as the value of the current property//and the callback function contains two parameters, the first number is the current property name, the second is the current property value//I think it would be better to modify this code: if (Callback.call (object, name, object[name] = = = = False) { if(Callback.call (Object[Name], name,Object[Name]) ===false) { //If you use return False in the callback callback function, the next loop is not executed Break; } } } //the one that needs to be traversed is an array Else { //here for the wording of a little BT, interpreted as://var value = object[0]; //For (; i < length;) { //if (false = = = Callback.call (value, I, value)) {//Break ; // } //value = Object[++i]; //} //again, I think it would be better to modify the code slightly://for (; i < length && false!== Callback.call (object, I, object[i++]);) { //} for(varValue =Object[0]; I < length && Callback.call (value, I, value)!==false; Value =Object[++i]) {} }}//This returns the object or array being traversed, but object is not changed, so $.each () is generally not assigned//But if it is used in the notation I modified in the comments, and the This (that is, a reference to object) is modified in the callback callback function//then the object returned here is the modified one or an array return Object; }