javascript 深拷贝,支持Array、Object、Date、RegExp、Function!
欢迎提出改正意见及建议!
function deepClone(o) {
let type = Object.prototype.toString.call(o);
//null
if (o === null) return null;
//函数
if (typeof o === 'function') return o.bind();
//基本数据类型
if (typeof o !== 'object') return o;
//日期
if (type === '[object Date]') return new Date(o);
//正则
if (type === '[object RegExp]') return new RegExp(o);
//数组或对象
let _o = new o.constructor;
for (let k in o) {
if (o.hasOwnProperty(k)) {
_o[k] = deepClone(o[k]);
}
}
return _o;
}