function clone(obj){
var result;
if (Array.isArray(obj)) {
result = [];
obj.forEach((item) => {
result.push(clone(item));
});
} else if (typeof obj === ‘object‘) {
result = {};
for (key in obj) {
result[key] = clone(obj[key]);
}
} else {
result = obj;
}
return result;
}
如果是Date或者RegExp之类的类型,就得另加判断了
来源: http://www.bubuko.com/infodetail-2050526.html