back to Overview

_.extend()

Copies every property of the source objects into the first object.
Util module only.

Syntax

_.extend(target, src...)

Parameters

target
the object to copy to
src
the object(s) to copy from. Variable argument, there can be any number of sources. Nulls and undefined parameters will be ignored.
(return value)
the target

Description

Copies every property of the source objects into the first object. The source objects are specified using variable arguments. There can be more than one. The properties are copied as shallow-copies.

Please note: Unlike jQuery, extend does not directly add a function to extend Minified, although you can use it to for this. To add a function to Minified lists, add a property to MINI.M. If you want to extend $ or _, just assign the new function(s) as property.

Example

Copying properties:

var target = {a:3, c: 3};
_.extend(target, {a: 1, b: 2}); // target is now {a: 1, b: 2, c: 3}

Example

Using several source values:

var extend = _.extend({a: 1, b: 2}, {a:3, c: 3}, {d: 5}); // target is now {a: 1, b: 2, c: 3, d: 5}

See also..

  • _.copyObj() is very similar to extend(), but with a slightly different and more straightforward syntax.
  • _.merge() copies a list of objects into a new object.

Comments

comments powered by Disqus

back to Overview

Functions