JQuery. Extend of jQuery. Fn. Extend of analysis

  • 2020-03-30 03:15:06
  • OfStack

Deep is a Boolean value that indicates whether or not to do a depth merge. The default is false and no depth merge is performed.
This way you can add new attributes and methods to jQuery or jquery.fn, as most other jQuery modules do.

Use $.extend() when adding an extension to jQuery
Such as jQuery. The extend ({add: function (a, b) {return a + b}})

Use: $. The add (1, 3) = = = = = > 4.

Use $.fn.extend() when adding an extension to a jQuery instance object;

$. Fn. The extend ({gys: function () {$(this). The CSS (" color ", "red")}});

Call: $(" div. Guo "). Gys ();

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201406/2014060910530010.jpg ">

Because the number of parameters is uncertain, no specific acceptable parameters are listed.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201406/2014060910530011.png ">

Options: points to a source object.
Name: the name of an attribute of a source object.
SRC: the original value of an attribute of the target object.
Copy: the value of a property of a source object
CopyIsArray: indicates whether the copy is an array
Clone: the modified value of the original value when copied in depth.
Target: target object.
I: the initial subscript of the source object.
Length: number of arguments used to correct the variable target.
Deep: whether to perform deep replication.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201406/2014060910530012.png ">

333~338: if the first parameter is a Boolean, assign the target to deep, the target retrieves the value, and the second parameter to target.
At this point, the value of I has changed from 1 to 2
327 lines of code gives you an idea of why this if statement exists.
And then fix it later.

341-343: if the target object is not an object or a function, targeted={};

When length is equal to I, no objcet1 is passed in... And so on,
At this point, take this current object (jQuery or jquery.fn) as the target object, and subtract I by one.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201406/2014060910530013.png ">

Line 351 starts the loop, and the I represents the index of the start source object, which is a very clever use.
Line 353 is also nice, putting the fetch and judgment of the source object in a statement that is executed only if the source object is not null.
Lines 354-362, the variable SRC is the original value, and the variable copy is the copy value.
If 360 lines are commented out, the following code will have an exception.
Var obj = {};
Obj. N1 = obj;
$. The extend (true, obj, {n2: obj});
In different browsers will issue different error, but without exception, the card screen appears for a long time.

Lines 365 to 372, recursively merge if it is a deep merge and the copy is a normal javascript object or array.
Lines 378-380, if not deeply merged, and copy is not undefined, override the property of the same name of the target object directly.


Related articles: