当前位置:网站首页>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 .
边栏推荐
- 防红域名生成的3种方法介绍
- Up meta - Web3.0 world innovative meta universe financial agreement
- Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
- Vxlan static centralized gateway
- About web content security policy directive some test cases specified through meta elements
- <No. 9> 1805. Number of different integers in the string (simple)
- 顶级域名有哪些?是如何分类的?
- zero-shot, one-shot和few-shot
- Attack and defense world ----- summary of web knowledge points
- What is an esp/msr partition and how to create an esp/msr partition
猜你喜欢
Hi3516 full system type burning tutorial
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
Epp+dis learning path (1) -- Hello world!
Airserver automatically receives multi screen projection or cross device projection
Processing strategy of message queue message loss and repeated message sending
Tutorial on principles and applications of database system (007) -- related concepts of database
ES底层原理之倒排索引
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
Tutorial on principles and applications of database system (009) -- conceptual model and data model
随机推荐
数据库系统原理与应用教程(009)—— 概念模型与数据模型
Apache installation problem: configure: error: APR not found Please read the documentation
Typescript interface inheritance
<No. 9> 1805. 字符串中不同整数的数目 (简单)
Several methods of checking JS to judge empty objects
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
H3C HCl MPLS layer 2 dedicated line experiment
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
【统计学习方法】学习笔记——第五章:决策树
Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
(待会删)yyds,付费搞来的学术资源,请低调使用!
BGP actual network configuration
开发一个小程序商城需要多少钱?
Minimalist movie website
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
【深度学习】图像多标签分类任务,百度PaddleClas
SQL Lab (41~45) (continuous update later)
什么是局域网域名?如何解析?