JSFiddle - React, Tailwind, and code Playground
by WilsonPage
JavaScript
// declare our function
var cloneExtend = function(o1, o2){
o2 = o2 || {};
for(p in o1){ o2[p] = o1[p] };
return o2;
}
// create an object
var myObject = {
a: 1,
b: 'string',
c: [1, 2, 3],
d: {
a: 1,
b: 2
}
}
// just clone myObject
var myClone = cloneExtend(myObject);
// extend and clone my object with new stuff
var myExtendedClone = cloneExtend(myObject, {e: 4} );
// add something new to the clone
myClone.f = 5;
// log both objects and notice they
// remain independant of one another
console.log(myObject);
console.log(myClone);
console.log(myExtendedClone);