JavaScript Object-oriented notation and jquery object-oriented writing

Source: Internet
Author: User

The origin of the article: the summary of jquery source Learning

In JS, the general object-oriented notation is as follows:

function The Cao () {}// defines a constructor function () {}// on the prototype Add initialization method function() {} // other practical methods to perform New Cao (); // Instantiate Object c1.init (); // Initialize c1.css (); // operation function

It is cumbersome to define an object Cao, instantiate C1,c1.init () initialization, and C1.other () to invoke other available methods, so that each call needs to instantiate C1 and then initialize C1.init () before other methods can be called.
jquery's approach is to:

function jQuery () {returnnewfunctionfunction( {}=   jquery.prototype;//This sentence can be ignored first, the following will be said JQuery (). other ();

Only one row

JQuery (). other ();

The code has done all the work, why?

Under Analysis:

Calling jquery () actually completes the instantiation and initialization process, because jquery () returns the initialization object of JQuery.prototype.init ().

Jquery->jquery.prototype

Jquery->jquery.prototype->jquery.prototype.init

Like the above relationship, the returned new JQuery.prototype.init (), on the prototype of JQuery, JQuery.prototype.init () does not have the other () method at all, How do you get JQuery.prototype.init () to operate other ()?

So there is this sentence:

JQuery.prototype.init.prototype = Jquery.prototype;

The phrase is to assign a reference to the jquery prototype to the JQuery.prototype.init prototype, as shown here:

Jquery->jquery.prototype

Jquery->jquery.prototype->jQuery.prototype.init,jQuery.prototype.init. prototype- >jquery.prototype

This gives a reference to the jquery prototype on the JQuery.prototype.init prototype.

The JQuery.prototype.init () returned by jquery () actually contains everything on the jquery object, so jquery (). Other () is the direct manipulation of other () on the jquery object.

Modifying a jquery prototype is a prototype that modifies the initialization init object.

The actual wording of the jquery source code:

#61-#64行:

function (Selector, context) {        // The JQuery object is actually just the Init constructor ' enhanced '         returnnew  jQuery.fn.init (selector, context, rootjquery);},    

What the hell is JQuery.fn.init?

There is such a word in #96:

Jquery.fn = Jquery.prototype = {...}

Description Jquery.fn is Jquery.prototype, the jquery prototype.

In #283:

JQuery.fn.init.prototype = Jquery.fn;

is JQuery.prototype.init.prototype = Jquery.prototype;

JavaScript Object-oriented notation and jquery object-oriented writing

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.