Home > Web Front-end > JS Tutorial > body text

jQuery study notes jQuery.extend(), jQuery.fn.extend() analysis_jquery

WBOY
Release: 2016-05-16 16:45:28
Original
1263 people have browsed it

deep is a Boolean value, indicating whether to perform deep merging. The default is false, and deep merging is not performed.
In this way, new properties and methods can be added to jQuery or jQuery.fn. Most other modules of jQuery This is how it is achieved.

Use $.extend()
when adding extensions to jQuery, such as: jQuery.extend({add:function(a,b){return a b}})

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

Use $.fn.extend();

when adding extensions to jQuery instance objects

$.fn.extend({gys:function(){$(this).css("color","red")}});

Call:$("div.guo").gys();

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

options: points to a certain source object.
name: a certain attribute name of a certain source object.
src: the original value of a certain attribute of the target object.
copy: a certain source object The value of an attribute
copyIsArray: indicates whether the copy is an array
clone: ​​the correction value of the original value during deep copy.
target: the target object.
i: the starting subscript of the source object .
length: the number of parameters, used to modify the variable target.
deep: whether to perform deep copy.

333~338: If the first parameter is a Boolean value, assign the target to deep. The target will re-obtain the value and assign the second parameter to the target.
At this time, the i value will change from 1 at the beginning. It becomes 2. It means that the source object originally started from the second element, and now it becomes the third element. This code combined with
327 lines of code will know why there is this if statement. The original function When extend is executed, regardless of the parameters, it first assigns values ​​to target and i.
and then corrects them later.

341~343: If the target object target is not an object or a function, then targeted={};

346~349: When length is equal to i, it means that no parameters such as objcet1,... are passed in.
At this time, this current object (jQuery or jQuery.fn) is used as the target object, and i is Decrease one. Thus the passed in object is treated as the source object.

Line 351 starts the loop, i represents the subscript of the starting source object, which is a very clever usage.
Line 353 is also wonderful, it puts the acquisition of the source object and the judgment of the source object in one statement , will be executed only when the source object is not empty.
Lines 354~362, the variable src is the original value, and the variable copy is the copied value. If the target and copy references are the same, an infinite loop will occur during traversal,
so there is no Will overwrite the properties of the target object with the same name. If you comment out line 360, an exception will occur in the following code.
var obj={};
obj.n1=obj;
$.extend(true,obj, {n2:obj});
Different error reports will appear in different browsers, but without exception, the screen will freeze for a long time.

Lines 365~372, if it is a deep merge, and the copy copy is an ordinary JavaScript object or array, then merge recursively.
Lines 378~380, if it is not a deep merge, and the copy is not undefined, the target object will be overwritten directly attribute of the same name.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!