In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the meaning of shallow copy in javascript". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the meaning of shallow copy in javascript"?
1. Simply copy the outermost attributes of the object without dealing with the deeper attributes of the object, which will cause the deep attributes of the copied object and the original object to point to the same memory.
2. If one object changes the address of memory, it will affect another object.
The object is serialized into a JSON string, and then the string is generated into a new object through JSON.parse () for deep replication.
Example
Let arr = [], obj = {a: 1} for (let I = 0; I < 3; iSuppli +) {arr.push (obj)} / / the values of basic variables are generally stored in stack memory, while the values of variables of object type are stored in heap memory, which stores the corresponding space address. Basic data types: Number, Boolean, Undefined, String, Null. Console.log (arr) / / the memory address is replaced to point to {console.log 2} console.log (JSON.parse (JSON.stringify (arr) / / [{aV1}] to prevent console.log (obj) / {aconsole.log 1} / / when we execute aV2, there is a new object in heap memory {aV2}. The b of the stack memory points to the new space address (pointing to {aposition 2}), while the original {aposition 1} in the heap memory will be garbage collected by the program engine, saving memory space. Obj.a = 2console.log (obj) / / {arr 2} console.log (arr) / / [{aVaV2}] console.log ("/ /") / / Deep copy if the attribute value of the object is of the general type (string, number), the new object obtained through Object.assign ({}, srcObj) is a deep copy let obj1 = {a: 1pm b: 2}; let obj2 = Object.assign ({}, obj1, {b: 20}) Console.log ('obj1', obj1); / / {a: 1obj2', obj2 b: 2} console.log (' obj2', obj2); / / {a: 1direction b: 20} console.log ("/ /") / / shallow copy if the attribute value is an object or other reference type, the new object obtained through Object.assign ({}, srcObj) is a shallow copy obj1 = {a: 1ZOB: {c: 2ZD: 3}} Obj2 = Object.assign ({}, obj1); obj2.a = 10 posiobj2.b.c = 20scape console.log ('obj1', obj1); / / {a: 1, b: {c: 20, d: 3}} console.log ("obj2", obj2) / / {a: 10, b: {c: 20, d: 3}} console.log ("/ /") / / Object.assign ({}) and Object.assign () with or without {} Var test1 = {a:'a'}, test2 = {b: {c: 1}}; / / Object.assign ({}) the original object will not be changed console.log (Object.assign ({}, test1, test2)) / / after merging, {a: "a", b: {console.log 1}} console.log (test1) / / {aburea'} console.log (test2) / / {b: {CRAV 1} / / Object.assign () the original object will be changed (only the overwritten original object test2 overlay test1) console.log (Object.assign (test1, test2)) / / after merging {a: "a", b: {console.log 1}} console.log (test1) / / {a: "a", b: {CRAV 1}} console.log (test2) / / {b: {CRAV 1}} / / when replacing the overlaid object test1 overrides test2console.log (Object.assign (test2, test1)) / / after merging {a: "a", b: {console.log 1}} console.log (test2) / / {a: "a"} this result is obtained when the above test2 overrides the test1 test comments. If you print without comments, you will get {a: "a", b: {CRAV 1}}, because the above test1= {a: "a", b: {c test2 1} console.log (test2) / / {a: "a" B: {cappl1}} so far I believe you have a deeper understanding of "what a shallow copy in javascript means". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.