Skip to the content.

$.extend([deep,] target [, source1] [, source2] [, …])

Merge the properties of one or more sources into a target object.

By default the merge is shallow — top-level properties are copied and any nested objects/arrays are shared by reference. Pass true as the first argument for a recursive deep merge in which plain objects and arrays are merged in place.

The own-property __proto__ is skipped to avoid prototype pollution.

Signatures

extend(deep: true, target: Record<string, unknown>, ...sources: Record<string, unknown>[]): Record<string, unknown>;
extend(target: Record<string, unknown>, ...sources: Record<string, unknown>[]): Record<string, unknown>;

Parameters

When called with a single argument (shallow only), the properties are merged onto the factory $ itself, mirroring jQuery’s $.extend(plugin) plug-in pattern.

Returns

For deep merges, the mutated target. For shallow merges, a new object.

Examples

import $ from "dabbyjs";
import "dabbyjs/utils/extend/extend";

// Shallow merge
const settings = $.extend({}, defaults, overrides);
import $ from "dabbyjs";
import "dabbyjs/utils/extend/extend";

// Deep merge — nested objects are recursed
const merged = $.extend(true, { ui: { theme: "light" } }, { ui: { dense: true } });
// merged.ui === { theme: "light", dense: true }
import $ from "dabbyjs";
import "dabbyjs/utils/extend/extend";

// Plug-in pattern — augment $ itself
$.extend({
    log(message: string) { console.log("[dabby]", message); },
});

See also