Home  >  Article  >  Web Front-end  >  The difference and use of jquery.extend and jquery.fn.extend

The difference and use of jquery.extend and jquery.fn.extend

巴扎黑
巴扎黑Original
2017-07-09 11:53:061066browse

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.

JAVASCRIPT:

<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.

jQuery Code:

var settings = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; jQuery.extend(settings, options);

Result:

settings == { validate: true, limit: 5, name: "bar" }

Description:

Merge defaults and options without modifying defaults.

jQuery code:

var empty = {}; var defaults = { validate: false, limit: 5, name: "foo" }; var options = { validate: true, name: "bar" }; var settings = jQuery.extend(empty, defaults, options);

Result:

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:

  1. ##

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!

Statement:
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