← Back to Index

$.assign()

Extends the target object by copying properties from source objects.

Examples

1. Shallow Merge

const target = { a: 1 };
const source = { b: 2 };
$.assign(target, source);

Result

// target is now:
{ a: 1, b: 2 }

2. Deep Merge

const defaults = { theme: { color: 'blue', font: 'arial' } };
const options = { theme: { color: 'red' } };
// Pass true for deep merge
const final = $.assign(true, {}, defaults, options);

Result

// final object:
{
     theme: {
          color: 'red',
          font: 'arial'
     }
}

3. Multiple sources

const obj = $.assign({}, {a:1}, {b:2}, {c:3});

Result

{ a: 1, b: 2, c: 3 }

Parameters

Parameter Type Description
deep boolean Optional. If true, the merge becomes recursive (deep copy).
target object The object to receive the new properties.
...sources object One or more objects containing the properties to merge.

Returns: The modified target object.