current position:Home>Simple sorting of JavaScript deep copy and shallow copy
Simple sorting of JavaScript deep copy and shallow copy
2022-04-29 05:20:20【InfoQ】
Basic data type
In the data structure
- The allocated space life cycle of stack on memory is very short , When the variable is used , The method is released after execution , So in js among , After the variable is used , Basically recycled ,
- There is an exception , In the case of closures , Variables always exist in memory and are not released .
- Stack storage has first in and last out , The characteristics of LIFO : 1,2,3,4,5,6 => 6,5,4,3,2,1
Reference data type
Reference data type and heap memory heap Some of the relationships
- stay JavaScript in , Direct access to locations in heap memory... Is not allowed , You cannot directly manipulate the heap memory space of an object .
- The reference address of the object is stored in stack memory , In our daily coding process , When manipulating objects , Read the reference address of the existing stack memory of the object, not the object in the heap , The values of reference types are accessed by reference .
JavaScript Simple schematic illustration of heap memory and stack memory in

Shallow copy - Deep copy
Shallow copy
let userInfo = {
name: "zhangsan",
age: "29",
say: function () {
console.log("hello");
},
child: [
{
name: "zhangsan01",
},
],
};
- Object to deconstruct , Only the first level objects can be copied
// Object to deconstruct ...
let info = { ...userInfo };
info.name = "lisi";
info.child.name = "lisi001";
info.say();
console.log("userInfo", userInfo);
console.log("info", info);
userInfo and info Medium child.name It's all changed to ---->"lisi001"
- Object.assign() The first layer is deep copy , After the secondary attribute is the shallow copy
let info = {};
Object.assign(info, userInfo);
info.name = "lisi";
info.child.name = "lisi001";
console.log("userInfo", userInfo);
console.log("info", info);
- JSON.parse(JSON.stringify()); Objects can be copied , But when the attribute is function when , Not copied to the new object , So in the daily development process , Involving array objects , Use JSON.parse(JSON.stringify()); No problem
let info = JSON.parse(JSON.stringify(userInfo));
info.name = "lisi";
info.child.name = "lisi001";
console.log("userInfo", userInfo);
console.log("info", info);
- for in, The first layer can be copied , The second layer is modified , Or the reference address used , The objects before and after have changed
let info = {};
for (let key in userInfo) {
info[key] = userInfo[key];
}
info.name = "lisi";
info.child.name = "lisi001";
console.log("userInfo", userInfo);
console.log("info", info);
Summary of shallow copy
Deep copy
- Use recursion to traverse each attribute , During recursive traversal , Process and copy for each data type
- lodash Deep copy method , Interested students , You can read lodash Deep copy of the implementation source code
- More ways , To be added
ending
- When data types are complex , All in the operation stack memory Stack Memory address of , Pointer to the object in heap memory heap The data of .
- The object passed in is using the object literal {} The object created is still the object generated by the constructor
- If the object is created by a constructor , Do you want to copy the attributes on the prototype chain
- If you want to copy the attributes on the prototype chain , Then, if there are multiple attributes with the same name on the prototype chain , Keep which
- Data type for , The data type of the property , Respective defects , Applicable business scenarios , Make your own wheels or Use native methods , Tool class
Source code address
Code cloud
https://gitee.com/lewyon/vue-note
githup
https://github.com/akari16/vue-note
copyright notice
author[InfoQ],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/116/202204261120132844.html
The sidebar is recommended
- Rich text editor: ckeditor (using ckeditor4 Vue)
- The get request of nginx agent only returns part of the data. The problem is solved
- JavaScript traverses the irregularly nested multi-layer objects, returns the map with "index", and realizes the rapid positioning of sub attributes
- HTTP keep alive details
- [technical update] http / 3 quic Foundation
- Vue to react ----- can the constructor be omitted when using ES6?
- Use of nested HTML
- Vue to react to realize slot function
- When to use react PureComponent
- React 16.6 memo()
guess what you like
Deep understanding of children https://segmentfault.com/a/1190000011527160
This paper solves the cross domain problem Vue + springboot caused by the separation of front and back ends
The difference between shallow copy and deep copy is to use native JavaScript to realize deep copy
Definition of Vue slot
Sorting algorithm in JavaScript
JavaScript implements search algorithm, sequential search and binary search
leetcode20. Valid parentheses, implemented using JavaScript
'interface' declarations can only be used in typescript files in Vue 3.0 + ts resolvent
496 next element larger JavaScript implementation leetcode
leetcode 682. Implementation of JavaScript in baseball game
Random recommended
- Source code analysis, Vue What happens when using (), take initializing vuex as an example
- JavaScript optional chain
- Source code analysis, Vue global API set, del, nexttick, use, mixin, etc
- Brief introduction of Vue source code directory design
- Docxtemplator + Vue + docxtemplator image module free realize the front-end Word Download Text and free image download
- vue 2. What happens when x source code analyzes new vue()
- Vue bidirectional binding principle
- Use and principle of Vue keepalive
- Properties and usage of vuex
- Communication mode between Vue components
- Front end interview Foundation
- Usage scenario and principle of Vue nexttick
- Configuration and use of rich text editor ckeditor4 in Vue
- How does Java randomly get elements from a list
- Study summary of pytext: a search path from NLP research to production
- html+css+JavaScript
- Tunnel http://bt3xna.natappfree.cc Invalid cannot connect to 172.0.0.1:8080 The web service powered by natapp. Is not available on this port cn
- Next JS using react quill
- Vue Gaode map can only have one start point and end point. How to realize it
- Divide and conquer strategy -- find the k-th smallest element in the array
- Singleton mode - front end design mode
- QT sets that child elements do not inherit the parent style
- Using react to realize comment function
- Front end Vue template
- Front end vuejs set sail
- Front end Vue data binding and MVVM
- Vue data proxy and hijacking in front end
- Vue style of front end
- Front end Vue event handling
- Principle of Vue monitoring data change in front end
- Vue calculation attribute and monitoring attribute of the front end
- Front end Vue conditional rendering and list rendering
- The front-end Vue collects form data
- Vue built-in instruction of front end
- Unknown HttpClient factory netty at org. openqa. selenium. remote...
- What is the gutter principle of Ant Design Vue a-row
- Element form account password
- CSS to add horizontal lines around the text
- Solution to vscode Vue project always reporting space or semicolon warning
- Calling setcookie in Ajax fails without any error in the IDE