当前位置:网站首页>Object. Simple implementation of assign()
Object. Simple implementation of assign()
2022-07-07 12:29:00 【Xiaoding Chong duck!】
One 、Object.assign()
Introduce
1. Definition :
Object.assign()
Method is used to assign the values of all enumerable properties from one or more source objects to the target objects . It will return the target object . ——MDN
2. grammar
Object.assign(target, ...sources)
target: Target audience
sources: Source object , You can have as many as you like
Return value : It must be an object
Two 、 Principle and Implementation
1. Analysis characteristics :
(1) The first parameter cannot be null or undefined( I'm not sure why )
(2) The return value must be an object , So no matter what type the first parameter passed in ( except null or undefined), Use both Object() Into objects
(3) Because you can pass multiple object parameters , Then traverse and assign a value to the target object .
2. Code :
function myAssign(target, ...sources) {
// The first parameter cannot be null or undefined
if (target === undefined || target === null) {
throw new TypeError('cannot convert first argument to object');
}
// What is returned must be an object
let res = Object(target);
for (let i = 0; i < sources.length; i++) {
let obj = sources[i];
if (obj !== null && obj !== undefined) {
for (let k in obj) {
if (Object.prototype.hasOwnProperty.call(sources[i], k)) {
res[k] = obj[k];
}
}
}
}
return res;
}
summary :
I will know why the attribute of the first layer is deep copy after I write it once , The objects in the object are all shallow copies .
In fact, it just takes out the attributes in the object and assigns them to the target object , But the object of the object is not traversed and assigned .
边栏推荐
- 源代码防泄密中的技术区别再哪里
- Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
- gcc 编译报错
- H3C HCl MPLS layer 2 dedicated line experiment
- File upload vulnerability - upload labs (1~2)
- 2022年在启牛开华泰的账户安全吗?
- 数据库系统原理与应用教程(011)—— 关系数据库
- About web content security policy directive some test cases specified through meta elements
- BGP actual network configuration
- 对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
猜你喜欢
Experiment with a web server that configures its own content
Hi3516全系统类型烧录教程
[pytorch practice] image description -- let neural network read pictures and tell stories
盘点JS判断空对象的几大方法
Hi3516 full system type burning tutorial
Minimalist movie website
The IDM server response shows that you do not have permission to download the solution tutorial
Solutions to cross domain problems
Review and arrangement of HCIA
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
随机推荐
[pytorch practice] image description -- let neural network read pictures and tell stories
Niuke website
什么是局域网域名?如何解析?
SQL Lab (46~53) (continuous update later) order by injection
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
Up meta - Web3.0 world innovative meta universe financial agreement
Routing strategy of multi-point republication [Huawei]
数据库系统原理与应用教程(008)—— 数据库相关概念练习题
30. Feed shot named entity recognition with self describing networks reading notes
《通信软件开发与应用》课程结业报告
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
利用栈来实现二进制转化为十进制
About web content security policy directive some test cases specified through meta elements
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
AirServer自动接收多画面投屏或者跨设备投屏
【PyTorch实战】用RNN写诗
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]
@Bean与@Component用在同一个类上,会怎么样?
Static vxlan configuration