Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

137 lines (126 sloc) 5.81 kb
<?xml version="1.0"?>
<entry type="method" name="jQuery.extend" return="Object">
<title>jQuery.extend()</title>
<signature>
<added>1.0</added>
<argument name="target" type="Object">
<desc> An object that will receive the new properties if additional objects are passed in or that will extend the jQuery namespace if it is the sole argument.</desc>
</argument>
<argument name="object1" type="Object" optional="true">
<desc>An object containing additional properties to merge in.</desc>
</argument>
<argument name="objectN" optional="true" type="Object">
<desc>Additional objects containing properties to merge in.</desc>
</argument>
</signature>
<signature>
<added>1.1.4</added>
<argument name="deep" optional="true" type="Boolean">
<desc>If true, the merge becomes recursive (aka. deep copy).</desc>
</argument>
<argument name="target" type="Object">
<desc>The object to extend. It will receive the new properties.</desc>
</argument>
<argument name="object1" type="Object">
<desc>An object containing additional properties to merge in.</desc>
</argument>
<argument name="objectN" optional="true" type="Object">
<desc>Additional objects containing properties to merge in.</desc>
</argument>
</signature>
<desc>Merge the contents of two or more objects together into the first object.</desc>
<longdesc>
<p>When we supply two or more objects to <code>$.extend()</code>, properties from all of the objects are added to the target object.</p>
<p>If only one argument is supplied to <code>$.extend()</code>, this means the target argument was omitted. In this case, the jQuery object itself is assumed to be the target. By doing this, we can add new functions to the jQuery namespace. This can be useful for plugin authors wishing to add new methods to JQuery.</p>
<p>Keep in mind that the target object (first argument) will be modified, and will also be returned from <code>$.extend()</code>. If, however, we want to preserve both of the original objects, we can do so by passing an empty object as the target:</p>
<pre><code>var object = $.extend({}, object1, object2);</code></pre>
<p>The merge performed by <code>$.extend()</code> is not recursive by default; if a property of the first object is itself an object or array, it will be completely overwritten by a property with the same key in the second object. The values are not merged. This can be seen in the example below by examining the value of banana. However, by passing <code>true</code> for the first function argument, objects will be recursively merged. (Passing <code>false</code> for the first argument is not supported.)</p>
<p>Undefined properties are not copied. However, properties inherited from the object's prototype <em>will</em> be copied over. Properties that are an object constructed via <code>new MyCustomObject(args)</code>, or built-in JavaScript types such as Date or RegExp, are not re-constructed and will appear as plain Objects in the resulting object or array.</p>
<p>On a <code>deep</code> extend, Object and Array are extended, but object wrappers on primitive types such as String, Boolean, and Number are not.</p>
<p>For needs that fall outside of this behavior, write a custom extend method instead. </p>
</longdesc>
<example>
<desc>Merge two objects, modifying the first.</desc>
<code><![CDATA[
var object1 = {
apple: 0,
banana: {weight: 52, price: 100},
cherry: 97
};
var object2 = {
banana: {price: 200},
durian: 100
};
/* merge object2 into object1 */
$.extend(object1, object2);
var printObj = typeof JSON != "undefined" ? JSON.stringify : function(obj) {
var arr = [];
$.each(obj, function(key, val) {
var next = key + ": ";
next += $.isPlainObject(val) ? printObj(val) : val;
arr.push( next );
});
return "{ " + arr.join(", ") + " }";
};
$("#log").append( printObj(object1) );
]]></code>
<html><![CDATA[
<div id="log"></div>
]]></html>
</example>
<example>
<desc>Merge two objects recursively, modifying the first.</desc>
<code><![CDATA[
var object1 = {
apple: 0,
banana: {weight: 52, price: 100},
cherry: 97
};
var object2 = {
banana: {price: 200},
durian: 100
};
/* merge object2 into object1, recursively */
$.extend(true, object1, object2);
var printObj = typeof JSON != "undefined" ? JSON.stringify : function(obj) {
var arr = [];
$.each(obj, function(key, val) {
var next = key + ": ";
next += $.isPlainObject(val) ? printObj(val) : val;
arr.push( next );
});
return "{ " + arr.join(", ") + " }";
};
$("#log").append( printObj(object1) );
]]></code>
<html><![CDATA[
<div id="log"></div>
]]></html>
</example>
<example>
<desc>Merge defaults and options, without modifying the defaults. This is a common plugin development pattern.</desc>
<code><![CDATA[
var defaults = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
/* merge defaults and options, without modifying defaults */
var settings = $.extend({}, defaults, options);
var printObj = typeof JSON != "undefined" ? JSON.stringify : function(obj) {
var arr = [];
$.each(obj, function(key, val) {
var next = key + ": ";
next += $.isPlainObject(val) ? printObj(val) : val;
arr.push( next );
});
return "{ " + arr.join(", ") + " }";
};
$("#log").append( "<div><b>defaults -- </b>" + printObj(defaults) + "</div>" );
$("#log").append( "<div><b>options -- </b>" + printObj(options) + "</div>" );
$("#log").append( "<div><b>settings -- </b>" + printObj(settings) + "</div>" );
]]></code>
<html><![CDATA[
<div id="log"></div>
]]></html>
</example>
<category slug="utilities"/>
<category slug="version/1.0"/>
</entry>
Jump to Line
Something went wrong with that request. Please try again.