当前位置:网站首页>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制作的贴图自动匹配材质球工具
- Upgrade from a tool to a solution, and the new site with praise points to new value
- 【PyTorch实战】用RNN写诗
- 利用棧來實現二進制轉化為十進制
- @Bean与@Component用在同一个类上,会怎么样?
- 关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
- 什么是局域网域名?如何解析?
- NGUI-UILabel
- What is an esp/msr partition and how to create an esp/msr partition
- VSCode的学习使用
猜你喜欢

Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition

千人规模互联网公司研发效能成功之路

About web content security policy directive some test cases specified through meta elements

Vxlan 静态集中网关

<No. 8> 1816. Truncate sentences (simple)

Static routing assignment of network reachable and telent connections
![110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]](/img/62/1ec8885aaa2d4dca0e764b73a1e2df.png)
110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
![[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer](/img/5f/75549fc328d7ac51f8b97eef2c059d.png)
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer

Processing strategy of message queue message loss and repeated message sending

解密GD32 MCU产品家族,开发板该怎么选?
随机推荐
VSCode的学习使用
gcc 编译报错
Simple network configuration for equipment management
<No. 9> 1805. Number of different integers in the string (simple)
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
Review and arrangement of HCIA
EPP+DIS学习之路(1)——Hello world!
BGP actual network configuration
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
利用棧來實現二進制轉化為十進制
2022 年第八届“认证杯”中国高校风险管理与控制能力挑战赛
(to be deleted later) yyds, paid academic resources, please keep a low profile!
@Bean与@Component用在同一个类上,会怎么样?
[statistical learning methods] learning notes - improvement methods
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
Routing strategy of multi-point republication [Huawei]
idea 2021中文乱码
Processing strategy of message queue message loss and repeated message sending
ENSP MPLS layer 3 dedicated line
Sonar:cognitive complexity