Learn all about the jQuery function jQuery.extend().
When two or more object arguments are supplied to
$.extend(), properties from all of the objects are added to the target object. Arguments that are
undefined are ignored.
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, you 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, you want to preserve both of the original objects, you can do so by passing an empty object as the target:
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 or subsequent 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.
false for the first argument is not supported.
Undefined properties are not copied. However, properties inherited from the object’s prototype will be copied over. Properties that are an object constructed via
deep extend, Object and Array are extended, but object wrappers on primitive types such as String, Boolean, and Number are not. Deep-extending a cyclical data structure will result in an error.
For needs that fall outside of this behavior, write a custom extend method instead, or use a library like lodash.