当前位置:网站首页>对象深复制,面试题
对象深复制,面试题
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);
边栏推荐
- Chapter 15 HMM模型
- 毕昇编译器优化:Lazy Code Motion
- Leetcode.25 K个一组翻转链表(模拟/递归)
- Install win7 virtual machine in Vmware and related simple knowledge
- Presto Event Listener开发
- Js fifteen interview questions (with answers)
- Sun Zhengyi lost 150 billion: it was expensive at the beginning
- Qt 消息机制和事件
- Five Star Holdings Wang Jianguo: Deepen the track with "plant spirit" and promote growth with "animal spirit"
- Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
猜你喜欢

反射机制篇

异常处理(try,catch,finally)

HUAWEI CLOUD escorts the whole process of "Wandering Ark" for the first time, creating a popular brand

leetcode:331. 验证二叉树的前序序列化

电脑系统重装后怎么用打印机扫描出文件?

The 2022-8-9 sixth group of input and output streams

Jinshanyun earthquake, the epicenter is in bytes?

Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut

leetcode:286.墙和门

【技术分享】SLA(服务等级协议)原理与配置
随机推荐
小程序+自定义插件的关键性
leetcode:325. 和等于k的最长子数组长度
【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
web 面试高频考点 —— 性能优化篇(手写防抖、手写节流、XXS攻击、XSRF攻击)
OSS文件上传
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
Qt message mechanism and events
UNI-APP_监听页面滚动h5监听页面滚动
C 在函数声明前加typedef
EasyExcel使用
18-GuliMall 压力测试与性能监控
Chapter 15 HMM模型
R语言使用mean函数计算样本(观测)数据中指定变量的相对频数:计算时间序列数据中大于前一个观测值的观测值所占的比例总体的比例
每日一R「02」所有权与 Move 语义
【软考 系统架构设计师】案例分析⑤ 质量属性和架构评估
用PLSQL导出Oracle一个表
Let's talk about what DDL, DML, DQL and DCL are in SQL statements
第 1 章 一大波数正在靠近——排序
leetcode brush questions diary Calculate the number of elements on the right that is less than the current element
一文让你快速了解隐式类型转换【整型提升】!