当前位置:网站首页>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 .
边栏推荐
- SQL blind injection (WEB penetration)
- Tutorial on principles and applications of database system (007) -- related concepts of database
- 关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
- 30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
- Tutorial on the principle and application of database system (011) -- relational database
- 【统计学习方法】学习笔记——支持向量机(上)
- 111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
- RHSA first day operation
- Configure an encrypted web server
- NGUI-UILabel
猜你喜欢
idm服务器响应显示您没有权限下载解决教程
Tutorial on principles and applications of database system (007) -- related concepts of database
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
[deep learning] image multi label classification task, Baidu paddleclas
Minimalist movie website
Static vxlan configuration
[pytorch practice] image description -- let neural network read pictures and tell stories
Static comprehensive experiment
Up meta - Web3.0 world innovative meta universe financial agreement
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
随机推荐
Completion report of communication software development and Application
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
How much does it cost to develop a small program mall?
Up meta - Web3.0 world innovative meta universe financial agreement
Attack and defense world ----- summary of web knowledge points
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
Apache installation problem: configure: error: APR not found Please read the documentation
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
Epp+dis learning path (1) -- Hello world!
消息队列消息丢失和消息重复发送的处理策略
How to understand the clothing industry chain and supply chain
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
Routing strategy of multi-point republication [Huawei]
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
Several methods of checking JS to judge empty objects
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
Idea 2021 Chinese garbled code
Upgrade from a tool to a solution, and the new site with praise points to new value