Jquery each () source code

Source: Internet
Author: User

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])

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.