JSFiddle - React, Tailwind, and code Playground
JavaScript
function deepClone(variable) {
if (variable instanceof Date) {
var clone = new Date();
clone.setFullYear(variable.getFullYear());
clone.setMonth(variable.getMonth());
clone.setDate(variable.getDate());
clone.setHours(variable.getHours());
clone.setMinutes(variable.getMinutes());
clone.setSeconds(variable.getSeconds());
clone.setMilliseconds(variable.getMilliseconds());
} else if (variable instanceof RegExp) {
var clone = new RegExp;
} else if (variable instanceof Error) {
var clone = new Error();
} else if (Object.prototype.toString.call(variable) == '[object Array]') {
var clone = [];
} else if (variable instanceof Function) {
var clone = variable.bind(clone);
} else if (variable instanceof Object) {
var clone = {};
} else {
return variable;
}
for (property in variable) {
if (variable.hasOwnProperty(property)) {
clone[property] = deepClone(variable[property]);
}
}
return clone;
}
var a = { x: 1, y: 2, z: [1, 2, 3], w: new Date(2014, 1, 1, 12, 0, 0) };
var b = deepClone(a);
b.x = 10;
console.log(a.x);
b.z.push(4);
console.log(a.z); // [1, 2, 3]
b.w.setFullYear(2015);
console.log(a.w.getFullYear());