JQuery1.5 redemption of plug-in mechanism of new features

Source: Internet
Author: User

It seems that jQuery1.5 has two major changes: jQuery subclass and asynchronous queuing. This article only talks about jQuery subclass.

First, we need to understand why we need to launch this function. Is it to strengthen its zero OO function? No. You should know from the source code below that it is not a custom class. But this is also a silly question, because this method is called Class instead of subclass.

 
 
  1. subclass: function(){    
  2.     function jQuerySubclass( selector, context ) {    
  3.         return new jQuerySubclass.fn.init( selector, context );    
  4.     }    
  5.     jQuerySubclass.superclass = this;    
  6.     jQuerySubclass.fn = jQuerySubclass.prototype = this();    
  7.     jQuerySubclass.fn.constructor = jQuerySubclass;    
  8.     jQuerySubclass.subclass = this.subclass;    
  9.     jQuerySubclass.fn.init = function init( selector, context ) {    
  10.         if (context && context instanceof jQuery && !(context instanceof jQuerySubclass)){    
  11.             context = jQuerySubclass(context);    
  12.         }    
  13.         return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass );    
  14.     };    
  15.     jQuerySubclass.fn.init.prototype = jQuerySubclass.fn;    
  16.     var rootjQuerySubclass = jQuerySubclass(document);    
  17.     return jQuerySubclass;    
  18. },  

I think its true intention is to improve its plug-in mechanism.

JQuery plug-ins are easy to develop. Adding a method to its prototype chain is called "plug-in". Therefore, there are many jQuery plug-ins and many junk plug-ins, since the update is stopped, there are also a lot of garbage.

 
 
  1. JQuery. fn. garbage =Function(){
  2. Alert ("This is a junk plug-in! ")
  3. }

Of course, this is in the ideal situation, the plug-in is as simple as a function is a lot of plug-ins ). For example, if you develop a plug-in and your colleagues develop a plug-in with the same name, a problem occurs. In this case:

 
 
  1. (Function($ ){// Plug-in 1 
  2. $. Fn. extend ({
  3. A:Function(){// Agent body 
  4. This. B ()
  5. },
  6. B:Function(){}// Auxiliary functions 
  7. })
  8. }) (JQuery );
  9. (Function(){// Plug-in 2 
  10. $. Fn. extend ({
  11. C:Function(){// Agent body 
  12. This. B ()
  13. },
  14. B:Function(){}// Auxiliary functions 
  15. })
  16. }) (JQuery );

At this time, the two plug-ins may be mixed. Plug-in 1 B and plug-in 2 B may not be one thing, which is very likely because plug-ins may be developed by cainiao in the United States, plug-in 2 is developed by cainiao in Germany, and foreigners like to put their plug-ins in the plug-in display area on the jQuery official website. It is easy for the Chinese lazy guy and cainiao to step on the thunder.

Plug-in chaos disaster as jQuery becomes increasingly popular, because jQuery is easy to learn and use, the company's front-end is not enough people, just grab a background. As a result, the front-end also experienced a bubble like the Chinese property market. A single page references N plug-ins. The full screen selector has a high maintenance cost and is almost impossible.

In the final analysis, the problem is that everyone is contaminated with jQuery and jQuery. fn, just as everyone once liked to pull shit in the global scope. The global scope is public toilets and it is inevitable to go there, but we can use subclasses to transfer the burden of jQuery and jQuery. fn.

In jQuery1.5, we should write the plug-in like this.

 
 
  1. var MyjQuery = jQuery.subclass();    
  2. MyjQuery.fn.writeHello = function(){ this.text('Hello World'); };    
  3. MyjQuery('p').writeHello();  

Because MyjQuery is a subclass of jQuery, it has all its capabilities, and chain operations are the same.

 
 
  1. MyjQuery('div').css('border', '1px red solid').writeHello();  

We can also modify the original jQuery method without modifying the source code due to some hidden bugs:

 
 
  1. MyjQuery. fn. text =Function(Val ){
  2. VarOrig = jQuery. fn. text;
  3. If(TypeofVal ="String"){
  4. ReturnOrig. call (This,"!! "+ Val +"!! ");
  5. }Else{
  6. ReturnOrig. apply (This, Arguments );
  7. }
  8. };
  9. MyjQuery ('. Aaa'). Text ("Text");
  10. MyjQuery ('. Aaa'). Text ();
  11. //!! Text !! 
  12. // Does not affect jQuery itself 
  13. JQuery ('. Aaa). text ('Text ');// Text 

Of course, this also brings about a problem. All plug-ins are bound to a new namespace and re-run to the global scope ...... It seems that the further salvation will wait until its package loading mechanism comes out. Therefore, it is essential to use less plug-ins and improve javascript (non-jQuery) capabilities.

Original article:Http://www.cnblogs.com/rubylouvre/archive/2011/01/21/1940935.html

  1. Keep improving the analysis and optimization of jQuery code
  2. Six major improvements to jQuery1.5: Simpler DOM operations
  3. JQuery entry: three types of operations on Arrays
  4. JQuery 1.5 first Beta release paid for download
  5. 10 awesome and practical jQuery image plug-ins for free attachment download
  6. JQuery 1.5 official version released 5 major changes eye-catching

Related Article

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.