jQuery: The Write Less, Do More JavaScript Library

Utilities/jQuery.extend

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Utilities

jQuery.extend( target, object1, [objectN] )

Extend one object with one or more others, returning the original, modified, object.
If no target is specified, the JQuery namespace itself is extended. This can be useful for plugin authors wishing to add new methods to JQuery.

If a boolean false is specified as the first argument, JQuery performs a deep copy, recursively copying any objects it finds. Otherwise, the copy will share structure with the original object(s).

Undefined properties are not copied. However, properties inherited from the object's prototype will be copied over.
Arguments:

targetObject
The object to extend.
object1Object
The object that will be merged into the first.
objectN (Optional)Object
More objects to merge into the first.

Examples:
Merge settings and options, modifying 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 the defaults.

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

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

NameType