类型包括:Number、String、Object、Arrary、Boolean
function clone(obj) {
let result;
switch (typeof obj) {
case 'undefined':
break;
case 'string':
result = obj + '';
break;
case 'number':
result = obj + 0;
break;
case 'boolean':
result = obj;
break;
case 'object':
if (obj === null) result = null;
else {
if (Object.prototype.toString.call(obj).slice(8, -1) === 'Array') {
result = [];
for (let i = 0; i < obj.length; i++) {
result.push(clone(obj[i]));//递归
}
}
else {
result = [];
for (let k in obj) {
result[k] = clone(obj[k]);
}
}
}
break;
default:
result = obj;
break;
}
return result;
}