Jquery.extend = JQuery.fn.extend = function () {
Define some variables
if () {} See if it is a deep copy case
if () {} See the parameter is correct
if () {} see if plug-in situation
for () {There may be multiple object conditions
if () {} Prevents circular references, such as $.extend (A, {name:a}); The case of circular references is excluded;
if () {} deep copy
else if () {} shallow copy
}
}
Knowledge Point carding:
1, jquery is a function, jquery () returns an object, the constructor of this object is JQuery.fn.init (), the constructor of the prototype, that is, JQuery.fn.init.prototype = Jquery.fn, and Jquery.fn = Jquery.prototype, so the prototype function of the paparazzi function jQuery.fn.init () is Jquery.prototype, Summary: The object returned after calling JQuery () is actually inherited from Jquery.prototype prototype;
2, jquery Use the same set of source code for the expansion of static and instance methods, jquery uses two kinds of extend form analysis:
Jquery.extend (): Expanding static methods
JQuery.fn.extend (): extended instance method
$.extend (), this points to $, which is the jquery function, add method Aaa:this.aaa, $.AAA ()
$.fn.extend (), this points to $.fn = Jquery.prototype, adding the method Aaa:this.aaa-$ () in the prototype. AAA (), you need to call the AAA method in the instance object
"When writing only one object argument, JQ expands the two forms of the plugin"
$.extend ({//Extension tool method
Aaa:function () {alert (1);},
Bbb:function () {alert (2);}
});
$.AAA (); Pop up 1
$.BBB (); Pop Up 2
$.fn.extend ({//Extended JQ instance method
Aaa:function () {alert (3);},
Bbb:function () {alert (4);}
});
$ (). AAA ();
$ (). BBB ();
"When writing to an object argument, the following objects are extended to the first object"
var a = {}; Extending the properties of object a
$.extend (A, {name: "Hello"}, {age:30});
alert (a); Popup {name: "Hello", age:30}
var a = {}; You can also make deep and shallow copies.
var b = {name: {age:30}};
$.extend (A, b); $.extend () make a shallow copy!
A.name.age = 20;
alert (b.name.age); Pop up 20
$.extend (True, A, b); Add parameter True to tell JQ I want to make a deep copy
A.name.age = 20;
alert (b.name.age); Pop up 30
3. About inheritance
JQ uses copy inheritance, more flexible;
Class inheritance: Using the new constructor for inheritance;
Prototype inheritance:??
The extend of jquery source code