Creation of jquery objects (i)

Source: Internet
Author: User

Tag: Type access return select Execute his SPAN init entry

In the general usage of jquery, the execution of "$ ()" Returns a JQuery object, which is defined in the source code:

... var function () {    returnnew= Jquery.prototype;

The following conclusions can be drawn:

1, JQuery.fn.init () is jQuery.prototype.init ();
2. The init () function is a constructor, which is a jquery object created by invoking it, defined in the prototype of jquery ();

... var function (){    ...     return  This  = Jquery.fn;

The following conclusions can be drawn:
1. The init () constructor finally returns a reference to the new object;
2. The prototype of the Init () is covered with the original of jquery ().

The question now is, why does jquery write like that?
Look again, our goal is to create a jquery class that contains a series of callable functions that instantiate this class to get the jquery object, which it can write:

var function () {    function() {    ...} var new jQuery ();

Obviously, jquery is not like this, he uses no new constructs, like this:

var function () {    returnnewfunction() {    ...} var function (){    ...     return  This  function() {    ...}

However, if this is the case, the jquery function is just an entry, and the Init function is the real implementation, so jquery puts Init () in Jquery.prototype (), which is:

var function () {    returnnewfunction() {    ...     function (){        ...         return  This ;     function (){    ...}

At this point, there is another problem: the newly created object cannot access the other properties in Jquery.prototype (), and the workaround is to pass the Jquery.prototype to JQuery.prototype.init.prototype

var function () {    returnnewfunction() {    ...     function (){        ...         return  This ;     = Jquery.prototype;

The entire process for creating a jquery object is as follows:

Call the $ () method, call the JQuery.prototype.init () constructor, and return different jquery objects, depending on the selector. The same method in different jquery objects is written in Jquery.prototype. Pass Jquery.prototype to JQuery.prototype.init.prototype so that the newly created object can access the generic method "

Creation of jquery objects (i)

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.