function deepClone(data) {
let result;
if(typeof data !== 'object'){
result = data
} else {
if(Array.isArray(data)){
result = [...data]
} else if(data.constructor && data.constructor === RegExp){
result = data
} else if(data === null){
result = null
} else {
for(let key in data){
if(data.hasOwnProperty(key)){
result[key] = deepClone(data[key])
}
}
}
}
return result;
}
polyfill
function type(obj) {
var toString = Object.prototype.toString;
var map = {
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Function]': 'function',
'[object Array]': 'array',
'[object Date]': 'date',
'[object Regexp]': 'regExp',
'[object Undefined]': 'undefined',
'[object Null]': 'null',
'[object Object]': 'object',
};
return map[toString.call(obj)];
}
function deepClone(data) {
var t = type(data), o, i, ni;
if (t === 'array') {
o = [];
} else if (t === 'object') {
o = {};
} else {
return data;
}
if (t === 'array') {
for (i = 0, ni = data.length; i < ni; i++) {
o.push(deepClone(data[i]))
}
return o;
} else if (t === 'object') {
for (var key in data) {
if (data.hasOwnProperty(key)) {
o[key] = deepClone(data[key]);
}
}
return o;
}
}