当前位置:网站首页>对象深复制,面试题
对象深复制,面试题
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);
边栏推荐
- NodeJS使用JWT
- 十步以内,用小程序快速生成App!
- R语言patchwork包将多个可视化结果组合起来、使用plot_annotation函数以及tag_level参数将组合图用大写字母进行顺序编码、为组合图的标签添加自定义前缀信息
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用labs参数自定义Y轴的轴标签文本(customize Y axis labels)
- 【LaTex】 Font “FandolSong-Regular“ does not contain requested(fontspec)Script “CJK“.如何抑制此种警告?
- ArrayList 和 LinkedList 区别
- 电脑系统重装后怎么用打印机扫描出文件?
- R语言ggplot2可视化:使用ggpubr包的ggscatter函数可视化散点图、使用scale_x_continuous函数的breaks参数指定X轴的断点的个数(设置参数n)
- 【Leetcode】2104. Sum of Subarray Ranges
- 你的 Link Button 能让用户选择新页面打开吗?
猜你喜欢
随机推荐
一文让你快速了解隐式类型转换【整型提升】!
【微信小程序开发(八)】音频背景音乐播放问题汇总
Sun Zhengyi lost 150 billion: it was expensive at the beginning
Qt message mechanism and events
CV复习:softmax代码实现
重装系统后新建文本文档打不开怎么办
PyQt5: Getting Started Tutorial
LeetCode_2632_字符串压缩
R语言修改dataframe数据列的名称:使用dplyr包的rename函数修改列名、使用colnmaes函数修改列名、在数据筛选的时候重命名列名
Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
Qt 消息机制和事件
Kubernetes Service对象
Socket发送缓冲区接收缓冲区快问快答
One Pass 2074: [21CSPJ Popularization Group] Candy
C. Binary String Reconstruction
【软考 系统架构设计师】案例分析④ 软件架构风格
Js fifteen interview questions (with answers)
OFDM 十六讲 7 - Inter-Symbol-Interference
深度学习100例 —— 循环神经网络(RNN)实现股票预测









