The jquery. Each method is used to traverse an array or object and process the elements currently traversed. jquery uses this function frequently. The following describes the function in detail:
Code
/* !
* Jquery source code analysis-each function
* Jquery version: 1.4.2
*
*----------------------------------------------------------
* Function Introduction
*
* The each function is appended to the jquery object through the jquery. Extend function:
* Jquery. Extend ({
* Each: function (){}
*});
* If you do not know the jquery. Extend function source code, you can refer to the document "jquery source code analysis-extend function ".
*
* The jquery. Each method is used to traverse an array or object and process the elements currently traversed.
* The jquery. Each method can add additional parameters to the handler function (the callback usage methods with and without parameters are inconsistent)
*
*----------------------------------------------------------
* Instructions
* The effect of the each function based on the parameter type is inconsistent:
* 1. Traverse objects (with additional parameters)
* $. Each (object, function (P1, P2 ){
* This; // This indicates the current attribute value of the object in each traversal.
* P1; P2; // additional access parameters
*}, ['Parameter 1', 'parameter 2']);
*
* 2. traverse the Array (with attachment parameters)
* $. Each (array, function (P1, P2 ){
* This; // This indicates the current element of the array in each traversal.
* P1; P2; // additional access parameters
*}, ['Parameter 1', 'parameter 2']);
*
* 3. Traverse objects (no additional parameters)
* $. Each (object, function (name, value ){
* This; // This indicates the value of the current property.
* Name; // name indicates the name of the current property of the object.
* Value; // value indicates the value of the current property of the object.
*});
*
* 4. traverse the Array (no additional parameters)
* $. Each (array, function (I, value ){
* This; // this points to the current element
* I; // I indicates the current subscript of Array
* Value; // value indicates the current element of the array.
*});
*----------------------------------------------------------
*
*/
// Jquery. Each (), $. Each ()
// @ Param {object} | {array} the object to be traversed or processed
// @ Param {function} callback traversal processing callback function
// @ Param {array} additional parameters of The args callback function
Each: Function (Object, callback, argS ){
// When an object needs to be traversed, the name variable is used to record the attribute name of the object.
VaR Name,
// When an array needs to be traversed, the I variable is used to record the array subscript of the loop.
I = 0 ,
// The length of the traversal array. The length of the array is stored when the object to be traversed is an array.
// If you want to traverse an object, the length = undefined
Length = Object. length,
// Check whether the object with the 1st parameters is an object.
// Exclude the array type based on object. length and the function type based on isfunction (because the function is also an object)
Isobj = Length === Undefined | Jquery. isfunction (object );
// When the callback function has additional parameters, execute the first branch
// If (!! ARGs ){
If (ARGs ){
// An object needs to be traversed.
If (Isobj ){
// Traverses object attributes. Name is the object attribute name, which has been declared at the top of the function.
// Many people are not used to the for (VAR name in object) method. If they are not declared, the name will be defined as a global variable.
For (Name In Object ){
// Call the callback function. The scope of the callback function is the value of the current attribute.
// For example: callback () {This; // This In the function points to the current property value
// Use the 3rd ARGs of each as an additional parameter of the callback function.
If (Callback. Apply (object [name], argS) === False ){
// If return false is used in the callback function, the next loop is not executed.
Break ;
}
}
}
// An array needs to be traversed.
Else {
// Loop Length. The loop variable I is defined at the top of the function.
// The auto-increment of the loop variable is executed inside the loop.
For (; I < Length ;){
// Call the callback function, which is consistent with the callback call described above.
// This in the callback function points to the current array element.
If (Callback. Apply (object [I ++ ], ArgS) === False ){
Break ;
}
}
}
}
// When the callback function does not have additional parameters, the second branch is executed.
Else {
// An object needs to be traversed.
If (Isobj ){
// The property name of the loop object, which is defined at the top of the Function
For (Name In Object ){
// Call the callback function
// In object traversal without parameters, the scope indicates the value of the current attribute.
// The callback function contains two parameters: the first number of current attribute names, and the second value is the current attribute value.
// I think this sentenceCodeIf (callback. Call (object, name, object [name]) === false ){
If (Callback. Call (object [name], name, object [name]) === False ){
// If return false is used in the callback function, the next loop is not executed.
Break ;
}
}
}
// An array needs to be traversed.
Else {
// Here the for statement is a bit BT, which is interpreted:
// VaR value = object [0];
// For (; I <length ;){
// If (false === callback. Call (value, I, value )){
// Break;
// }
// Value = object [++ I];
// }
// Similarly, I think it is better to modify the code here:
// 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]) {
}
}
}
// The traversal object or array is returned, but the object is not changed. Therefore, $. Each () is not assigned.
// However, if I use the modified method in the annotation and modify this (that is, Object Reference) in the callback function
// The returned object is the modified object or array.
Return Object;
}