jQuery.extend()
jQuery.extend( target [, object1] [, objectN] ) Returns: Object
Description: Merge the contents of two or more objects together into the first object.
-
version added: 1.0jQuery.extend( target [, object1] [, objectN] )
target 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.
object1An object containing additional properties to merge in.
objectNAdditional objects containing properties to merge in.
-
version added: 1.1.4jQuery.extend( [deep], target, object1 [, objectN] )
deepIf true, the merge becomes recursive (aka. deep copy).
targetThe object to extend. It will receive the new properties.
object1An object containing additional properties to merge in.
objectNAdditional objects containing properties to merge in.
When we supply two or more objects to $.extend()
, properties from all of the objects are added to the target object.
If only one argument is supplied to $.extend()
, 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.
Keep in mind that the target object (first argument) will be modified, and will also be returned from $.extend()
. If, however, we want to preserve both of the original objects, we can do so by passing an empty object as the target:
var object = $.extend({}, object1, object2);
The merge performed by $.extend()
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 true
for the first function argument, objects will be recursively merged.
Undefined properties are not copied. However, properties inherited from the object's prototype will be copied over. For performance reasons, properties that have values of 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.
Note: When performing a deep extend, Object and Array are extended, however primitive types such string, boolean and number are not. For specific needs that fall outside of this behaviour, it is recommended to write a custom extend method as this will be significantly faster from a performance perspective.
Examples:
Example: Merge two objects, modifying the first.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
</head>
<body>
<div id="log"></div>
<script>
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 = 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) );
</script>
</body>
</html>
Example: Merge two objects recursively, modifying the first.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
</head>
<body>
<div id="log"></div>
<script>
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 = 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) );
</script>
</body>
</html>
Example: Merge defaults and options, without modifying the defaults. This is a common plugin development pattern.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
</head>
<body>
<div id="log"></div>
<script>
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 = 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>settings -- </b>" + printObj(settings) + "</div>" );
$("#log").append( "<div><b>options -- </b>" + printObj(options) + "</div>" );
</script>
</body>
</html>