当前位置:网站首页>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 .
边栏推荐
- 111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
- 数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
- 小红书微服务框架及治理等云原生业务架构演进案例
- ES底层原理之倒排索引
- [Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
- <No. 8> 1816. 截断句子 (简单)
- (to be deleted later) yyds, paid academic resources, please keep a low profile!
- idm服务器响应显示您没有权限下载解决教程
- Vxlan 静态集中网关
- Utiliser la pile pour convertir le binaire en décimal
猜你喜欢
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
ENSP MPLS layer 3 dedicated line
(to be deleted later) yyds, paid academic resources, please keep a low profile!
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Tutorial on the principle and application of database system (011) -- relational database
Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
The road to success in R & D efficiency of 1000 person Internet companies
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
Hi3516 full system type burning tutorial
Completion report of communication software development and Application
随机推荐
Solutions to cross domain problems
AirServer自动接收多画面投屏或者跨设备投屏
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
千人规模互联网公司研发效能成功之路
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
The road to success in R & D efficiency of 1000 person Internet companies
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
Idea 2021 Chinese garbled code
Hi3516全系统类型烧录教程
Configure an encrypted web server
Simple network configuration for equipment management
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
VSCode的学习使用
(待会删)yyds,付费搞来的学术资源,请低调使用!
College entrance examination composition, high-frequency mention of science and Technology
OSPF exercise Report
顶级域名有哪些?是如何分类的?
How to use PS link layer and shortcut keys, and how to do PS layer link
跨域问题解决方案
防红域名生成的3种方法介绍