RingoJS

Module ringo/utils/objects

Adds utility functions for working with JavaScript Objects

Functions


clone (object, cloned, recursive)

Copies the properties of an object into a target object.

Example

let objects = require("ringo/utils/objects");
let a = [1, 2, 3];
let b = {"a": a};
let c = {};

// shallow clone: b.a and c.a will share the same array
objects.clone(b, c);

// this modifies all three: a, b.a, and c.a
b.a[0] = 99;
console.dir(a); // --> [ 99, 2, 3 ]
console.dir(b); // --> { a: [ 99, 2, 3 ] }
console.dir(c); // --> { a: [ 99, 2, 3 ] }

// reset to original values
a = [1, 2, 3];
b = {"a": a};
c = {};

// c is now a deep clone of b
objects.clone(b, c, true);

// this modifies only a and b.a
b.a[0] = 99;

console.dir(a); // --> [ 99, 2, 3 ]
console.dir(b); // --> { a: [ 99, 2, 3 ] }

// c.a stays untouched, holds the original values
console.dir(c); // --> { a: [ 1, 2, 3 ] }

Parameters

Object object

the object to clone

Object cloned

optional clone object

Boolean recursive

pass true to create a deep clone. Otherwise a shallow clone is created.

Returns

Object

the clone object


merge (obj...)

Creates a new object as the as the keywise union of the provided objects. Whenever a key exists in a later object that already existed in an earlier object, the according value of the earlier object takes precedence.

Example

const a = { "k1": "val-A" };
const b = { "k1": "val-B", "k2": "val-B" };
const c = { "k1": "val-C", "k2": "val-C" };

// result: { k1: 'val-A', k2: 'val-B' }
const result = objects.merge(a, b, c);

Parameters

Object... obj...

The objects to merge