jquery has two methods for developing plug-ins, namely:
JQuery.fn.extend ();
Jquery.extend ();
Jquery.fn
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 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:
jQuery
. Extend ({min: function(a, b) { return a < b ? a : b; },
max: function(a, b) { return a > b ? a : b; }
});
jQuery.min(2,3); // 2 jQuery.max(4,5); // 5
OBJECTJ query.extend (Target, Object1, [objectn])
Extend an object with one or more other objects, returning the object being extended
var settings = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; jQuery
. Extend (Settings , options);
result: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:
$.fn.extend ({ Alertwhileclick:function () { $ (This). Click (function () { alert ($ (this). Val ()); }); } }); $ ("#input1"). Alertwhileclick (); //on the page:
$ ("#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.
The call to Jquery.extend () does not extend the method to an instance of the object, and the method that references it also needs to be implemented by the JQuery class, such as Jquery.init (), and JQuery.fn.extend () The call extends the method to the object's prototype, so it has these methods when instantiating a jquery object, which is important 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.
(function ($) {$.fn.tooltip = function (options) {};//equivalent to var tooltip = {function (options) {}};$.fn.extend (tooltip) = $.pro Totype.extend (tooltip) = $.fn.tooltip}) (JQuery);
Original link: Understand jquery $.extend (), $.fn and $.fn.extend () Copyright, reprint, please specify the source, offenders must investigate.
Annotated source format: front-end Development blog (http://caibaojian.com/jquery-extend-and-jquery-fn-extend.html)
Understanding jquery's $.extend (), $.fn, and $.fn.extend ()