In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to use assign in es6". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In es6, assign is used for object merging to copy all enumerable attributes of the source object to the target object; if the target object has the same name attribute as the source object, or multiple source objects have the same name attribute, the latter attribute overrides the previous attribute with the syntax of "Object.assign (...)".
This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.
What is the use of assign in es6
The Object.assign method is used for merging objects, copying all enumerable properties (key:value) of the source object (source) to the target object (target).
For example:
Const target = {target,source1,source2 1}; const source1 = {bazaar 2}; const source2 = {CRAV 3}; Object.assign (target,source1,source2) / / target {avell 1, bv2, CRAV 3}
Note: if the target object has an attribute with the same name as the source object, or if more than one source object has an attribute with the same name, the following properties override the previous properties.
For example:
Const target = {avell1 const target 1}; const source1 = {brange2 Magi c: 2}; const source2 = {CRAV 3}; Object.assign (target,source1,source2); / / target {aVl1 direction blo 2JI CRAV 3}
If there is only one parameter, Object.assign will directly return the modified parameter. If the parameter is not an object, it will transfer out the object first, and then return. Since null and undefined cannot be converted into objects, an error will be reported if they are used as parameters. If the non-object parameter appears at the location of the source object (meaning not the first parameter), the processing rules are different. First of all, these parameters will be converted to objects, and if null or undefined appears, as long as you make sure that the first parameter is not returned, there will be no error.
For example:
Const obj = {aobj 1}, Object.assign (obj) = = obj / / true typeof Object.assign (2) / / object Object.assign (undefined) / / error Object.assign (null) / / error Object.assign (obj,undefined)
Other types of values (that is, numeric, string, and Boolean values) are not in the first parameter and will not report an error, but the string is copied into the target object as an array, and none of the other values has any effect.
Const v1 = 'abc'; const v2 = true; const v3 = 10; const objCurrent = Object.assign ({}, v1meme v2pje v3); / / {0purpurajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajiajianglangjiaoguo (2GRV); / / in the above code, v1JEV2JEV3 is a string, a Boolean value, a numeric value, respectively. As a result, only the string matches the target object (in the form of a string array), and the numeric value and Boolean value are ignored. This is because only the wrapper object of the string produces enumerated properties. The problem of deep and shallow copy of Object.assign () is const obj1 = {aVl1}; const obj2 = {BRV 2}; const obj3 = {CRAV 3}; const obj = Object.assign (obj1,obj2,obj3); console.log (obj); / / {aVl1 dint blv 2cLV 3} console.log (obj1) / / {the original object has also been changed, const v1 = {JSON.stringify 1}, const currentObj = Object.assign (JSON.parse (JSON.stringify (v1)), {JSON.stringify (v1)) console.log (currentObj) / / {a1Part 2} console.log (v1) / / {aV1} has not changed.
When there is only a first-level attribute in an object and there is no secondary attribute, this method is a deep copy, but when there is an object in the object, this method is a shallow copy after the secondary attribute.
Use recursion to achieve deep copy
/ / _ deep deep copy method function _ deep (source) {let target If (typeof source = = 'object') {/ / determine whether the target value is an array or an object target = Array.isArray (source)? []: {} for (let key in source) {/ / indicates whether the specified property if (source.hasOwnProperty [key]) is included in the object's own property. / / assign a value if the property is not an object Responsible for recursive if (typeof source [key]! = 'object') {target [key] = source [key]} else {target [key] = _ deep (source[ key])}} }} else {target = source} / / return the target value return target}
Deep copy using js
Function _ deepJs (_ data) {return JSON.parse (JSON.stringify (_ data));} "how to use assign in es6" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.