Per Closure documentation:
Extends an object with another object. This operates 'in-place'; it does not create a new Object. Example: var o = {}; goog.object.extend(o, {a: 0, b: 1}); o; // {a: 0, b: 1} goog.object.extend(o, {b: 2, c: 3}); o; // {a: 0, b: 2, c: 3}
But this is not a functional approach and for many contexts a functional approach is better. Does Closure offer something more modern for this?
e.g.
goog.object.extend(a, b);
becomes
a = goog.object.extend(a, b);
You can create a new object by using an empty object literal to be extended with the objects that you want to copy-merge:
var x = {};
goog.object.extend(x, a, b);
a = x;
If you are extending non-plain objects, you might need to use Object.create(Object.getPrototypeOf(a))
.
You could implement it yourself:
var a = {};
var b = extend(a, { a: 0, b: 1 });
var c = extend(b, { b: 2, c: 3 });
console.log(a, b, c);
function extend(a, b) {
var result = {};
for (var x in a) if (a.hasOwnProperty(x)) result[x] = a[x];
for (var x in b) if (b.hasOwnProperty(x)) result[x] = b[x];
return result;
}
Hope that helps.