Home > Article > Web Front-end > The difference and use of jquery.extend and jquery.fn.extend
The
$.extend() method has two uses in JQuery, the first is an extension method, and the second is implemented with jquery InheritanceWay
1>Extension method
jQuery.extend
The extension of the jQuery object can be understood as a static method, which is global and can be used without an instance of jQuery.
<code>jQuery.extend({ <br> min: function(a, b) { return a < b ? a : b; },<br/> max: function(a, b) { return a > b ? a : b; }<br> });</code>
2> Inheritance method of jQuery implementation
jQuery.extend( [deep], target, object1, [objectN])
Return value: Object
is obtained by merging 2 objects The new target, deep is optional (RecursiveMerge)
Merge settings and options, modify and return settings.
var settings = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; jQuery.extend(settings, options);
settings == { validate: true, limit: 5, name: "bar" }
Merge defaults and options without modifying defaults.
var empty = {}; var defaults = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; var settings = jQuery.extend(empty, defaults, options);
settings = = { validate: true, limit: 5, name: "bar" }<br>empty == { validate: true, limit: 5, name: "bar" }<br><br>
jQuery .fn.extend
对jQuery元素的扩展,只能用在jQuery元素上,可以理解为普通方法。定义插件时需要返回this,以支持jQuery的链式操作。
JAVASCRIPT:
The above is the detailed content of The difference and use of jquery.extend and jquery.fn.extend. For more information, please follow other related articles on the PHP Chinese website!