jquery Source Reading Program-August 3-1/40

Source: Internet
Author: User

1.jQuery no new Build

What we want to do is: we just have to write var $ = jQuery (), we can create a new tool object, and we can get the same effect if we want var $ = new JQuery ().

The key to doing this is to write all the tool methods in Jquery.prototype, and return and use the same object as new when building without new.

What do you do to return the same object?

The first thing you should think of when returning an object is return new xxx ();

So where does this xxx () be written?

Should be written in Jquery.prototype, because we want to return the object is able to manipulate all the tools and properties, so the location of xxx should be in the jquery.prototype inside

    • To implement no new build, the jquery object itself must first be a function
    • This function returns an object generated by an Init constructor in its own prototype.
    • The returned object should be a Jquery.prototype object (but not the same object)
    • But the problem is that this point in return this in the Init constructor is not what we want Jquery.prototype
    • The workaround is simple, just point the prototype of Init to Jquery.prototype, and the object returned by the function jquery can get all the properties and methods of Jquery.prototype

2. Chained calls

Very simply, all methods return this after the operation is complete.

3. Plug-in interface

The so-called plug-in interface is actually the Extend method, this method is not used to specifically access plug-ins.

This method is only used to extend the properties and methods of an object.

Both jquery and Jquery.fn have this approach (implementation is exactly the same), where this method of jquery is often used to extend the object, and this method on the Jquery.fn is used to access the new method of the plugin.

There are a few things to note about the implementation of extend:

jquery Source Reading Program-August 3-1/40

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.