Copy codeThe Code is as follows:
// Args is for internal usage only
Each: function (object, callback, args ){
Var name, I = 0,
Length = object. length,
IsObj = length = undefined | jQuery. isFunction (object );
If (args ){
If (isObj ){
For (name in object ){
If (callback. apply (object [name], args) === false ){
Break;
}
}
} Else {
For (; I <length ;){
If (callback. apply (object [I ++], args) === false ){
Break;
}
}
}
// A special, fast, case for the most common use of each
} Else {
If (isObj ){
For (name in object ){
If (callback. call (object [name], name, object [name]) === false ){
Break;
}
}
} Else {
For (var value = object [0];
I <length & callback. call (value, I, value )! = False; value = object [++ I]) {}
}
}
Return object;
},
Analysis: The jquery document says that each (callback) is used to execute a function using every matching element as the context. It is to use each to traverse the array and execute the same method.
The most important thing to implement this method is: call and apply usage: call (apply) is to change the object of the function from the initial context to the object pointed to by thisObj,
That is to say, thisObj is used to replace the original object to execute the method: the first parameter of call and apply is the object pointed to by this, and the subsequent parameters are passed to the function,
Parameters passed to the function by call are separated by commas (,), while apply is an array.
// 1. callback. apply (object [name], args)
// 2. callback. call (object [name], name, object [name])