Object.cascade()
Cascade given number of objects into a new, single object. Right-most argument takes precedence. Similar to _.extend however this does not mutate given arguments.
by davidhong
JavaScript
Object.cascade = function( /* object, ... */ ) {
var result = Object.create(null);
var args = Array.prototype.slice.call(arguments);
var each = Array.prototype.forEach;
each.call(args, function(source) {
for (var key in source) {
result[key] = source[key];
}
});
return result;
};
// TEST
var me = {
name: 'David',
age: 28,
attr: {
height: 181,
weight: 70
}
};
var patch = {
sex: 'Male',
age: 27
};
var patched = Object.cascade(me, patch);
console.log(patched);