当前位置:网站首页>对象深复制,面试题
对象深复制,面试题
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);
边栏推荐
猜你喜欢

Five Star Holdings Wang Jianguo: Deepen the track with "plant spirit" and promote growth with "animal spirit"

DXF笔记:文字对齐的研究

月薪5K的运维小白如何成为月薪5W的高级架构师?

typedef和#define的花里胡哨的用法

Swift 需求 如何防止把view重复添加到win里面

leetcode:321. 拼接最大数

Liver all night to write a thirty thousand - word all the commands the SQL database, function, speaks clearly explain operators, content is rich, proposal collection + 3 even high praise!

为什么这么多人都想当产品经理?

chart.js面积图曲线图统计插件

leetcode:286.墙和门
随机推荐
每日一R「02」所有权与 Move 语义
Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
CGLIB源码易懂解析
电脑系统重装后怎么用打印机扫描出文件?
(转)字符集编码标识符,数字表示字符编码
c:forEach varStatus属性
C. Binary String Reconstruction
【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
Five Star Holdings Wang Jianguo: Deepen the track with "plant spirit" and promote growth with "animal spirit"
【软考 系统架构设计师】案例分析④ 软件架构风格
Swift 需求 如何防止把view重复添加到win里面
[WeChat applet development (8)] Summary of audio background music playback problems
Chatting embarrassing scenes, have you encountered it?Teach you to get the Doutu emoticon package with one click, and become a chat expert
R语言拟合ARIMA模型并使用拟合模型进行预测推理:使用forecast函数计算ARIMA模型未来值(包含时间点、预测值、两个置信区间)
LeetCode_2632_字符串压缩
OFDM 十六讲 7 - Inter-Symbol-Interference
【Apifox】为什么如此受青睐,此篇文章和大家分享
leetcode:321. 拼接最大数
Flask's routing (app.route) detailed
Qt message mechanism and events