1.Query for the development of the plug-in arch two methods, namely:
1.jquery.extend (object); To extend the JQuery class itself. Adds a new method to the class.
2.jquery.fn.extend (object); Adds a method to a JQuery object.
Jquery.fn
Jquery.fn = Jquery.prototype = { init:function (selector, context) { // content }}
2.Jquery.fn = Jquery.prototype. O (∩_∩) o haha ~, to this prototype (prototype) is not strange!!
3. Although JavaScript There is no clear concept of a class, but it is more convenient to use a class to understand it. jquery is a well-encapsulated class, such as we use the statement $ ("#div1") 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.
Chestnut ①
jquery.extend ({ min:function (A, b) { return a < b? a:b; }, max:function (A, B ) { return a > B? a:b; }}); Jquery.min (23// Jquery.max (45 // 5
JQuery.fn.extend (object);
is to add a "member function" to the jquery class. An instance of the jquery class can call this "member function".
Chestnut ②
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:
$.fn.extend ({ alertwhileclick:function () { $ (this). Click (function () { alert ($ ( this). Val () );}); //$ ("#input1") is jquery's instance, call this extension method $ ("#input1"). Alertwhileclick ();
The call to Jquery.extend () does not extend the method to an instance of the object, and the method that references it is also implemented by the JQuery class, such as Jquery.init ()
The call to JQuery.fn.extend () extends the method to the prototype of the object, so when instantiating a jquery object, it has these methods in jquery. JS in the everywhere this point
jQuery.fn.extend = JQuery.prototype.extend
You can extend an object into the prototype of jquery, which is the plugin mechanism .
Chestnut ③
(function ($) { = function (options) {}; // equivalent to Var tooltip = { function (options) {} }; = $.prototype.extend (tooltip) = $.fn.tooltip}) (jQuery);
Excerpt from:http://caibaojian.com/jquery-extend-and-jquery-fn-extend.html
JQuery $.extend () Usage Summary