当前位置:网站首页>对象深复制,面试题
对象深复制,面试题
2022-08-09 22:05:00 【勇敢*牛牛】
function cloneDeep(source,target){
if(!target){
switch(true){
case source instanceof Function:
var arr=source.toString().replace(/\n/g,"").match(/.*\((.*?)\)\s*\{(.*)\}/).slice(1);
target=new Function(arr[0],arr[1]);
break;
case source instanceof HTMLElement:
target=source.cloneNode(true);
break;
case source instanceof Array:
target=new source.constructor();
break
case source instanceof Date:
case source instanceof Set:
case source instanceof Map:
case Boolean(source.buffer):
target=new source.constructor(source);
break
case source instanceof RegExp:
target=new RegExp(source.source,source.flags);
break
default:
target={
};
}
}
// console.log(target)
// var names=[...Object.getOwnPropertyNames(source),...Object.getOwnPropertySymbols(source)];
var names=Reflect.ownKeys(source);
for(var i=0;i<names.length;i++){
var key=names[i];
if(key==="prototype"){
var con=target.prototype.constructor;
Object.defineProperties(target.prototype,Object.getOwnPropertyDescriptors(source.prototype));
Object.defineProperty(target.prototype,"constructor",{
value:con});
continue;
}
var desc=Object.getOwnPropertyDescriptor(source,key);
if(desc.value instanceof Object){
Object.defineProperty(target,key,{
enumerable:desc.enumerable,
configurable:desc.configurable,
writable:desc.writable,
value:cloneDeep(desc.value)
})
}else{
Object.defineProperty(target,key,desc);
}
}
return target;
}
var o1=cloneDeep(obj);
边栏推荐
猜你喜欢
Basic JSON usage
Good future, want to be a second new Oriental
Presto Event Listener开发
How do task flow executors work?
Common commands and technical function modules of Metasploit
leetcode:323. 无向图中连通分量的数目
大型分布式存储方案MinIO介绍,看完你就懂了!
Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut
重装系统后新建文本文档打不开怎么办
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
随机推荐
Vmware中安装win7虚拟机以及相关简单知识
leetcode:286.墙和门
18-GuliMall 压力测试与性能监控
迅为瑞芯微RK3399开发板设置Buildroot文件系统测试MYSQL允许远程访问
README_Albumentations
mysql 、pg 查询日期处理
信息系统项目管理师---第十一章项目风险管理历年考题
异常处理(try,catch,finally)
Kubernetes Service对象
工作经验-组件封装(拖拽排序组件)
leetcode:332. 重新安排行程
typedef和#define的花里胡哨的用法
迁移学习 & 凯明初始化
关于ETL的两种架构(ETL架构和ELT架构)
APS系统能消除造成生产和运输延迟的瓶颈
xlrd 与 xlsxwritter 的基本操作
NodeJS使用JWT
MySQL——JDBC
C. Omkar and Baseball
Common commands and technical function modules of Metasploit