In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the implementation methods of deep copy of es6". In daily operation, I believe many people have doubts about the implementation of deep copy of es6. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the implementation methods of deep copy of es6?" Next, please follow the editor to study!
The implementation of deep copy: 1, use "Object.assign ({}, obj)" statement to achieve; 2, use "JSON.parse (JSON.stringify (obj))" statement to achieve; 3, use "$.extend (true, [], arr)" statement to achieve.
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
What is a deep copy
Deep copy is relative to shallow copy, the main difference is in reference type, in essence, shallow copy simply copies the reference address in the stack, so when you modify the newly copied value, the copied object will also be modified by you. Deep copy creates space for new objects in heap memory, so the copied object will not be modified for no reason.
How to realize Deep copy in es6
Method 1: use Object.assign
Object.assign makes deep copies of objects by default, but we need to note that it only makes deep copies of the outermost objects, that is, when objects are nested within them, the nested objects make shallow copies.
Function cloneDeepAssign (obj) {return Object.assign ({}, obj)}
(warm reminder: in the array copy method, using..., slice, concat, etc., to copy is the same effect, only deep copy the outermost layer)
At the same time, we know that Object.assign aims at the enumerable properties of the object itself, but has no effect on those that cannot be enumerated.
Therefore, when we are for a hierarchical single object, we can consider this method, simple and fast. (tried and does not support undefined)
Method 2: using JSON
This is the most frequently mentioned way of deep copy, most of which can be solved by JSON. The essence is that JSON will build new memory to store new objects.
Function cloneDeepJson (obj) {return JSON.parse (JSON.stringify (obj))}
But what we should pay attention to is:
Ignore undefined and symbol
Function cannot be copied because the JSON format string does not support Function and is automatically deleted when serialized
Built-in types such as Map, Set, RegExp, Date, ArrayBuffer and other built-in types are lost when serializing
Copies of circular reference objects are not supported; (circular references can be roughly understood to mean that the value of a property in an object is itself)
Method 3: take advantage of jQuery's $.extend ()
Var array = [1, 2, 3, 4]; var newArray = $.extend (true, [], array)
Obviously, the biggest disadvantage is that we still need to introduce the jQuery library, so we don't use it very often.
At this point, the study of "what are the implementation methods of deep copy of es6" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.