extend object
by John Allan
JavaScript
function isObject(o) {
return o === Object(o) && !Array.isArray(o);
}
function extend(to, from) {
for(k in from) {
if (from.hasOwnProperty(k)) {
if (isObject(from[k])) {
to[k] = extend({}, from[k]);
} else {
to[k] = from[k];
}
}
}
return to;
}
let o = {
foo: 'bar',
'hello there': 'steve',
more: {
stuff: 'here'
}
};
let n = extend({}, o);
console.log(n);
o.more.stuff = 'there';
console.log(n);