JQuery1.3.2 source code learning 7: setArray, each function

Source: Internet
Author: User

119 // Take an array of elements and push it onto the stack
120 // (returning the new matched element set)
121 pushStack: function (elems, name, selector ){
122 // Build a new jQuery matched element set
123 var ret = jQuery (elems );
// Add the old object onto the stack (as a reference)
126 ret. prevObject = this;

128 ret. context = this. context;

130 if (name = "find ")
Ret. selector = this. selector + (this. selector? "": "") + Selector;
Else if (name)
Ret. selector = this. selector + "." + name + "(" + selector + ")";

// Return the newly-formed element set
Return ret;
137 },

The pushStack method starting with row 121 is used to format a set passed through an array into a standard jQuery object, so that operations can be performed using the method provided by jQuery. 123rd convert the array into a jQuery object. See row 91.
Row 3 points to the original array object through the prevObject attribute, and row 3 saves the current environment reference.
Row 130 checks whether further query is performed on the array. If a query is provided, the current selector is updated.
Row 3: returns the constructed jQuery object.

// Force the current matched set of elements to become
// The specified array of elements (destroying the stack in the process)
// You shoshould use pushStack () in order to do this, but maintain the stack
142 setArray: function (elems ){
// Resetting the length to 0, then using the native Array push
// Is a super-fast way to populate an object with array-like properties
This. length = 0;
Array. prototype. push. apply (this, elems );

Return this;
},
The setArray method of row 142 is used to replace the query result in the current jQuery object with the array passed in through parameters.

// Execute a callback for every element in the matched set.
// (You can seed the arguments with an array of args, but this is
// Only used internally .)
154 each: function (callback, args ){
Return jQuery. each (this, callback, args );
},
The each method is used to traverse the query results. The method accepts a callback function. jQuery traverses the query results. this callback function is called for each query result object and this and parameter reference are passed through parameters.
For specific each methods, see the definition of the 671-line function.

671 each: function (object, callback, args ){
672 var name, I = 0, length = object. length;
673
674 if (args ){
If (length = undefined ){
For (name in object)
If (callback. apply (object [name], args) === false)
Break;
679} else
680 for (; I <length ;)
If (callback. apply (object [I ++], args) === false)
Break;

// A special, fast, case for the most common use of each
685} else {
686 if (length = undefined ){
687 for (name in object)
If (callback. call (object [name], name, object [name]) === false)
Break;
690} else
691 for (var value = object [0];
692 I <length & callback. call (value, I, value )! = False;
Value = object [++ I]) {}
693}

695 return object;
696 },
The first parameter of Each is a jQuery object. The second parameter is the callback function for processing Each object. The third parameter can provide an additional parameter for the callback function.
Note: If the callback function returns false, the traversal process ends.

The second row determines whether additional parameters are provided. If no additional parameters are provided, execute the common traversal processing in the else statement section of the second row.
Row 3 checks whether the object provides the length attribute. If not, Row 3 uses the for in statement to directly traverse all the members of the object. for each attribute, callback is used for processing, the call method is used to pass the current object as this to the callback function.
When rows 691 and 692 have the length attribute, they can be traversed directly by length.
If a parameter array is passed in, it is forwarded to row 3 for processing.
Check whether the object has the length attribute. If the for in statement is not used, use the subscript for traversal.
Row 680 processes the length attribute. In this case, the length of the array is directly traversed. Because the parameter uses the array method, the current object is used as this by applying, other parameters are called as arrays.

Source: blog

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.