jquery has two methods for developing plug-ins, namely:
JQuery.fn.extend (); Jquery.extend ();
Although JavaScript does not have a clear class concept, it is more convenient to use a class to understand it.
jquery is a well-encapsulated class, such as we use the statement $ ("#btn1") to generate an instance of the jquery class.
Jquery.extend (object); Adding a class method to the jquery class can be understood as adding a static method. Such as:
1 Jquery.extend ({2Minfunction(A, B) {returnA < b?a:b;},3Maxfunction(A, B) {returna > B?a:b;}4 });5Jquery.min (2,3);//26Jquery.max (4,5);//57 8 objectjquery.extend (Target, Object1, [objectn]) extends an object with one or more other objects, returning the extended object9 varSettings = {validate:false, Limit:5, Name: "foo" }; Ten varOptions = {validate:true, Name: "Bar" }; One jquery.extend (settings, options); AResult: Settings = = {validate:true, Limit:5, Name: "Bar"}
JQuery.fn.extend (object); The extension to Jquery.prototype is to add "member functions" to the jquery class. An instance of the jquery class can use this "member function".
For example, we want to develop a plugin, make a special edit box, when it is clicked, then alert the contents of the current edit box. You can do this:
1 $.fn.extend ({2Alertwhileclick:function() { 3$( This). Click (function(){ 4Alert ($ ( This). Val ()); 5 }); 6 } 7 }); 8$ ("#input1"). Alertwhileclick ();//on the page is:9$ ("#input1") is a jquery instance, and when it calls the Member method Alertwhileclick, it implements the extension, which pops up the contents of the current edit each time it is clicked.
Transferred from: http://blog.sina.com.cn/s/blog_7c5d61f30101da1k.html
The role and difference of $.extend and $.fn.extend of jquery