当前位置:网站首页>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 .
边栏推荐
- AirServer自动接收多画面投屏或者跨设备投屏
- NGUI-UILabel
- Customize the web service configuration file
- <No. 9> 1805. 字符串中不同整数的数目 (简单)
- 跨域问题解决方案
- [Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
- SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
- Processing strategy of message queue message loss and repeated message sending
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- Completion report of communication software development and Application
猜你喜欢

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

Solutions to cross domain problems

对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景

Configure an encrypted web server

Static comprehensive experiment

数据库系统原理与应用教程(007)—— 数据库相关概念

小红书微服务框架及治理等云原生业务架构演进案例

【PyTorch实战】用RNN写诗

Idea 2021 Chinese garbled code

《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
随机推荐
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
gcc 编译报错
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
Zhimei creative website exercise
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
Configure an encrypted web server
消息队列消息丢失和消息重复发送的处理策略
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
小红书微服务框架及治理等云原生业务架构演进案例
Minimalist movie website
Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
Up meta - Web3.0 world innovative meta universe financial agreement
<No. 9> 1805. Number of different integers in the string (simple)
RHSA first day operation
Upgrade from a tool to a solution, and the new site with praise points to new value
Error in compiling libssl
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
【统计学习方法】学习笔记——支持向量机(下)
Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
【统计学习方法】学习笔记——提升方法