JQuery customizes jQuery. extend () and jQuery. fn. extend () method. jQuery. the extend () method can create global functions or selectors, whereas jQuery. fn. the extend () method can create jQuery object methods.
For example:
JQuery. extend ({showName: function (name) {alert (name) }}); jQuery. showName (" ");
In addition to creating plug-ins, jQuery. extend () can also be used to expand jQuery objects.
For example:
var a = { name : "blue", pass : 123}var b = { name : "red", pass : 456, age : 1}var c = jQuery.extend({},a,b);
C has the attributes of object a and object B. Because object B is after object a, its name attribute takes precedence over object c.
The jQuery. extend () method transmits series options for the plug-in, including default values.
Function fn (options) {var options = jQuery. extend ({// default parameter Option List name1: value1, name2: value2, name3: value3}, options ); // use the function parameters to overwrite or merge them to the default parameter options list. // function body} fn ({name1: value3, name2: value2, name3: value1 }); // use the new value fn ({name4: value3, name5: value2}); // Add the new option fn () by default; // retain the default option value
When this method is called, a new parameter value will overwrite the default parameter option value. Otherwise, the default parameter value will be used.
How to Create a JQuery object using a JQuery. fn object
You can use the jQuery. fn object to add attributes and methods. In fact, the jQuery. fn object is attached to jQuery. prototype, which is abbreviated by jQuery.
What is fn. It is not difficult to see jQuery code.
JQuery. fn = jQuery. prototype = {
Init: function (selector, context ){//....
//......
};
It turns out that jQuery. fn = jQuery. prototype. It is certainly no stranger to prototype.
For example:
JQuery. fn. test = function () {alert ("this is the jQuery object method! ");} JQuery (" div "). click (function () {$ (this ). test (); // call the test () method on the current jQuery object });
We can call the jQuery. fn. extend () method to create the jQuery object method.
JQuery. fn. extend ({test: function () {return this. each (function () {alert (this. nodeName)}) ;}}); jQuery ("body *"). click (function () {$ (this ). test (); // call the jQuery object method });
In a word, jQuery. extend is a custom extension of the JQuery class, And jQuery. fn. extend is a custom extension of the JQuery object.