Jquery.pushstack () prototype method source analysis

Source: Internet
Author: User

This analysis is different from the previous approach, although Pushstack is also one of the prototype methods, but we almost never use the page call, in the reference manual there is no use of this method, but this method is very important, in the other way to use a lot of jquery will implicitly call this method:

It provides support for the following methods:
? ? JQuery object traversal:. EQ (),. First (),. Last (),. Slice (),. Map ().
? ? DOM Finds, filters:. Find (),. not (),. filter (),. closest (),. Add (),. Andself ().
? ? DOM traversal:. Parent (),. Parents (),. Parentsuntil (),. Next (),. Prev (),. Nextall (),. Prevall (),
. Nextunit (),. Prevunit (),. siblings (),. Children (),. Contents ().
? ? DOM insert: Jquery.before (), Jquery.after (), Jquery.replacewith (),. append (),. Prepent (),
. before (),. After (),. ReplaceWith ().

Below is the 1.7.1 version of the source code

    //Take an array of elements and push it onto the stack    //(returning the new matched element set)Pushstack:function(elems, name, selector) {//Build A new jQuery matched element set        varRET = This. Constructor (); if(Jquery.isarray (Elems)) {push.apply (ret, elems); } Else{jquery.merge (ret, elems); }        //ADD the old object onto the stack (as a reference)Ret.prevobject = This; Ret.context= This. Context; 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        returnret; },

In the English comment, we have already told us what this method is, that is, to put the array of elements of the acquired element back into the stack and return a new matched array, that is, after executing the jquery constructor, it will produce an instance object with matching elements in it. When you use this method, you create a new, empty jquery object, and then put the element that matches the object that called the method onto the stack, which is an array and then point to the object that called the method by a pointer, so that a chain stack can be extended continuously. Can let us achieve "back" operations, such as the End () method, the following analysis source:

? ? Parameter elems: An array of elements (or class array objects) that will be placed into the new JQuery object.
? ? Parameter name: The JQuery method name that produces an array of elements elems.
? ? Parameter selector: Parameters passed to the JQuery method for correcting the prototype properties. Selector.

// Build A new jQuery matched element set        var this. Constructor ();

Constructs a new empty jquery object Ret,this.constructor points to the constructor jquery (), ret is an empty jquery object

if (Jquery.isarray (Elems)) {            push.apply (ret, elems);         Else {            jquery.merge (ret, elems);        }

Determine if Elems is not an array, if you are not using merge (subsequent analysis) to convert the group, if it is put in RET, At this time the Elems is an array can not be directly ret.push otherwise it will become a two-dimensional array, here is also a clever use of the array parameters of the application of the property directly add an array of elements to another array group, very good way to quickly collection! This enables the data to be transferred

// ADD the old object onto the stack (as a reference)        this;

Since it is chained, you must have a pointer to add a property to the new object to point to the current jquery object

if (name = = = "Find" ) {            this. selector? "": ") +elseif  (name) {            this. Selector +". "+ Name +" ("+ Selec Tor + ")";}

Add a selector attribute to ret, where the selector is not the selection string represented by the prototype property selector, which is analyzed earlier, and its main function is to debug conveniently, you can see the method name and the old object's selection string and the incoming parameter selector, As for why the Find method is listed separately, it's time to wait until the Find method is analyzed.

// Return the newly-formed element set        return ret;

Finally, the new jquery object is returned, in fact, this method of code is not very much the idea is difficult to understand, for this call under the structure:

<!doctype html>        console.dir ($ (' a '). Parent (). Parent (0));     </script>

Then look at the elemental body.

This is easy to understand, this time again to see the way the Stack end ()

    function () {        returnthis. Constructor (null);    },

The original method is to find the prevobject, according to the above structure diagram is not difficult to see

$ (' a '). Parent (). Parent (0). End ()

The result of the operation is pointing to the DIV element, both of which are used in conjunction with each other, except that the end method is used in the page swagger and is used by the reference manual, and pushstack can only be a hero behind the scenes.

Jquery.pushstack () prototype method source analysis

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.