jquery Framework Learning

Source: Internet
Author: User

I have seen the source of the jquery library before, there are many analysis tutorials on the jquery framework, but recently want to take some time to re-learn. Since the advent of jquery has been favored by the vast majority of front-end developers, for many front-end developers, the road to the front end is not from writing native JavaScript, but basically from writing jquery began, and even for the backend developers, For them, jquery is also easy to get started with. So why would jquery be so popular, my personal opinion,

First, jquery encapsulates a number of methods for manipulating the DOM and some tool methods that are perfectly compatible with browser compatibility and are compatible with IE6 browsers before jquery 2.0. Developers don't need to write dozens of lines of code for browser compatibility, just one line of code to use jquery. For the era of PC-side prevalence, jquery is a perfect solution to the overwhelming majority of front-end developers.

Second, jquery's framework design, no new build way, and friendly chain calls make jquery very concise.

Here's a no-new build of jquery:

Usually we create a JS class using the form of prototypes and constructors:

function JQuery () {...} Jquery.prototype = {constructor:jquery,init:function () {},get:function () {} ...} var $ = new JQuery () $.get ();

Here we define the property in the constructor and define the method on the prototype. To invoke the Get method on the prototype, we first instantiate the constructor, new jquery, and then invoke the method on the prototype, which is obviously not the case with jquery. We used to write jquery at the time of the chain call does not use the New keyword at all. So let's take a look at the jquery source code:

var rootjquery;
var jQuery = function (selector, context) {return new JQuery.fn.init (selector, context, rootjquery);},jquery.fn = Jquer Y.prototype = {
Constructor:jquery, Init:function (selector, context, Rootjquery) {... return this; }}jquery.fn.init.prototype = Jquery.fn;rootjquery = JQuery (document);

The above for jquery implementation of the core code without the new structure, let us analyze the source of this section. First, the first paragraph defines the JQuery constructor, which returns an object new JQuery.fn.init (selector, context, rootjquery), The class for this object is Jquery.prototype.init, then there is no method on the Init prototype, and if we directly mobilize the prototype method on jquery will directly error. This is done by re-assigning the prototype of jquery to JQuery.fn.init's prototype, so that the jquery constructor is a method that can be called directly from the Jquery.fn.init,jquery prototype. So how is the chain transfer implemented, just add the return this to each called function and return the Jquery.fun.init object.

It's confusing to see this code before, but now it looks like the ingenious change to the Init method on the prototype will allow for a no-new build of jquery.

At present, the front-end framework prevails, many of the new front-end students are from the front-end framework, these MVVM framework, do not require developers to understand the DOM principle, data driven, change the virtual DOM, virtual Dom and document, the actual DOM to compare differences, so that the page Dom update. For the entry, whether from the writing of native JS, or jquery, or the front end of the framework can be, but for the future must learn jquery this Dom operation Library, and then learn the native implementation method, so as to really understand the DOM principle. No matter what kind of technology stack to use later, when problems arise, you will not have a face confused. Finally, the front-end framework in a single page application, and Dom too many pages, the implementation of this data binding does have a great advantage, but she is not necessarily suitable for all scenarios, for some large companies, some of the bottom of the old business, because of the high cost of migration, and for a friendly user experience, Also need to be compatible with the lower version of the browser, or to use jquery, Zepto and other operations Dom library.

jquery Framework Learning

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.