First, from a simple example, do not need a method with parameters to start
Create an anonymous function
The code is as follows |
Copy Code |
(function ($) { Attach a new method to jquery (see note 1 for details) $.fn.extend ({ Name of the plugin Myfirstname:function () { Iteration Current matching element collection Return This.each (function () { var obj = $ (this); Your own code }); } }); ) (JQuery);
|
Note 1: To understand the difference between $.fn.extend and $.extend, presumably the former incorporates the MyFirstName method into the instance object of jquery, such as $ ("#txtmy"). Add (3,4) calls methods like this, The latter is merging the MyFirstName method into the global object of jquery, such as $.add (3,4); This calls the method
Detailed distinction See
jquery has two methods for developing Plug-ins, respectively:
JQuery.fn.extend (object);
Jquery.extend (object);
Jquery.extend (object); To extend the jquery class itself. Adds a new method to the class.
JQuery.fn.extend (object); Add methods to the JQuery object.
What is FN? See the jquery code, it's not hard to find.
Jquery.fn = Jquery.prototype = {
Init:function (Selector, context) {//....
//......
};
The original Jquery.fn = Jquery.prototype. It's certainly not strange to prototype.
Although JavaScript does not have a well-defined class concept, it is more convenient to use classes to understand it.
jquery is a very nicely encapsulated class, for example, we use statement $ ("#btn1") to generate an instance of a jquery class.
Jquery.extend (object); Adding a class method to the jquery class can be understood as adding a static method. Such as:
$.extend ({
Add:function (a,b) {return a+b;}
});
Add a "static method" for jquery, and then you can use this method in the place where jquery is introduced,
$.add (3,4); Return 7
JQuery.fn.extend (object); The extension to Jquery.prototype is to add a "member function" to the jquery class. Examples of jquery classes can use this "member function".
Two, have the parameter of
code is as follows |
copy code |
//Create an anonymous function (function ($) { //Add a new method to jquery (see note 1 for details) $.fn.extend ({ //plug-in name Myfirstname:function () { //define default parameters br> Var parms={ parms1:1, Parms2:2 } and/or merge user-transmitted parameters and default parameters, returned to options (see note 2) var options = $.extend ( defaults, options); //iteration of the current matching element collection Return This.each (function () { ///Assign the merged parameter to O var o= options; //iteration Current matching element var obj = $ ( this); //Own Code }); } }); ) (jQuery); |
Remarks 2:var options = $.extend (defaults, options) means to combine defaults with options if the latter has the same element as the former name, the latter covering the former, and then merging to defaults, The defaults is then assigned to options, if the var options = $.extend ({},defaults, options), then the former and the latter are merged to the {} parameter and then assigned to the options, The structure and values of the defaluts have not changed.