The Extend () method merges the properties of two or more objects into the first object, and most of the subsequent functions of jquery are extended through the function,
When the argument has only one object, the object's properties are added to the jquery object.
The form of extension in jquery: 2 ways
$.extend ({//Extension Tool MethodsAaa:function() {alert (10); }, BBB:function() {alert (20) } }); $.fn.extend ({//extending the jquery instance approachAaa:function() {alert ("FN 1"); }, BBB:function() {alert ("FN 2"); } }); $.extend (); This-$ This. AAA-$.aaa (); $.fn.extend (); This$.fn (prototype) This. AAA--$ (). AAA ()
When there are multiple objects in the argument, the subsequent objects are extended to the first object:
var a = {name: "Linda"}; $.extend (a,{name:"Joke", Age:20},{address: "China"}); Console.log (a); // {name= "Joke", age=20, address= "China"}
With deep and shallow copies, jquery is a shallow copy by default
Shallow copy:varA = {name: "Linda"}; varb = {firends:[' aa ', ' BB ', ' CC ']}; $.extend (A, b); A.firends.push (' DD '); Console.log (b); //{firends:[' aa ', ' BB ', ' cc ', ' DD '}Deep Copy:varA = {name: "Linda"}; varb = {firends:[' aa ', ' BB ', ' CC ']}; $.extend (true , A,b); The first parameter is set to True, which is a deep copy of A.firends.push (' DD '); At this point the modification of the reference type of a has no effect on B console.log (b); //{firends:[' aa ', ' BB ', ' cc ']}
Decomposition of the source code:
function () { define some variables
if () {} See if it is a deep copy case
if () {} See if the parameters are correct
if () {} see if there is a plug-in condition for () { There may be multiple object conditions
if () {} prevents circular references
if () {} deep copy
else if () {} Shallow copy
}
jquery source Note (iv): Jquery.extend=jquery.fn.extend