JS memory life cycle, stack memory and stack memory, what is the shallow copy?

This article mainly introduces the JS memory life cycle, stack memory and heap memory, what is the shallow copy, has a certain reference value, and friends needed can be referred to. I hope that everyone has a big gain after reading this article. Let’s take a look at you with everyone.

JS memory life cycle

Distribute memory

  • Memory Read and Write
  • Stack memory and heap memory
  • JS data type
  • Before the stack memory and the stack of memory, everyone should know that JS is divided into two data types:

Basic data type

String, Number, Boolean, NULL, Undefined, Symbol

(size fixed, lightweight, relatively simple)

  • Object, Array, Function
    (Size is not necessarily, the space is large, relatively complex)

    Memory storage mechanism

  • VAR A = true; / / Boolean, basic data type var b = ‘jack’; // character type, basic data type var C = 18; // numeric, basic data type var D = {name: ‘jack’}; // object, Reference data type var D = [0, 1, 2, 3, 4, 5]; // array, reference data type copy code

    is because the data type is different, so Their storage method is also different, and it is completely different from the residence of the poor and the rich in real life (long). Let’s first look at a picture:

  • A


is all basic data types, D
 and  E  are all reference data types They have essential differences in the storage mode, 
The value of the basic data type is stored in the stack memory, and the value of the reference data type is stored in the stack memory, and the ink memory is only stored in the stack. The reference in the stack memory (ie, the address in the stack memory), like its name, reference data type

The memory access mechanism

is stored above, Next, the variable is accessed, the basic data type can directly access the value of the variable from the stack memory, and the reference data type must first find its corresponding reference address from the stack, and then hold this reference address, go to the plot Find, in order to get the value of the variable Shar-shallow copy Shallow copy The above has said that the basic data type and reference data type Different in storage, then, this deep copy, I want everyone to meet, old way, first to see a code in the interview VAR name = ‘jack’; var = {agn: 24}; var namecopy = name; var objcopy = obj; Namecopy = ‘bob’; objcopy.age = 15; console.log (name); //jackconsole.log. AGE); // 15 Copy code

You will find that


is not affected, and we name is modified


, Why will it affect


, this is because of the shallow copy problems in the ghost, first look at the following picture
  • So this happens because JS is for basic types and reference classes.Type, when we copy the reference type, copy the reference address of the object, so when

    var objcopy = obj;

    , Obj
     will be  Reference address  is copied to 
    Objcopy , so the two objects actually point to the same object, that is, change

    Objcopy , also changed OBJ The value, we call this situation as shallow copy , only copy the reference to the object, did not open up new memory, shorter, copying too much. (Only a shallow copy of the reference type)

will then look at the next section VAR name = ‘jack’; var obj = {agn: 24}; var namecopy = name; var objcopy = json.parse (json.stringify (obj)); Namecopy = ‘bob’; Objcopy .age = 15; console.log (name); //jackconsole.log (obj.age); // 24 Copy code It can be found that after json. Parse (json.stringify (obj)) After the transfer, the shallow copy does not exist, this wave is deep copy , the deep copy has opened up new stack memory addresses, and references the object Point to the newly opened memory address, and the objects replicated in front are completely independent, self-recovery, copying the ground, learn the skills to learn home, Self-reliable portal feel.

  • Further implementation of a deep copy method (more ways, please Baidu)

    VAR OBJCOPY = Object.assIgn ({}, obj); // object deep copy method Object.assignvar arraycopy = array.concat (); // array deep copy method compat () (Non-nested object or array) var ARRAYCOPY = array . Slice (); // A array of deep copy methods slice () Non-nested objects or arrays can be used) Json.Parse (Json.Stringify (array)) // By taking, json.parse (json.stringify ))) A common copy code
       The above array is easy to step on the pit, to see an example 

    VAR array = [ {Name: 'jack'}, ['Old']]; var arr = array.concat (); arrpy [0] .name = 'new'; console.log (array); // [{name: 'new '}, [' Old ']] Console.log (Arrcopy); // [{Name:' New '}, [' OLD ']] Copy Code can be clear See (in the case where the nesting object or array is available

  • and
    is valid)
       Thank you for reading this article carefully, I hope that Xiaobian share the JS memory life cycle, the stack memory and the stack of memory, what is the content of the shallow copy is helpful, and I also hope that everyone will support Tumi cloud, pay attention to Tumi Yun Industry Information Channel, I will find TUMI clouds, the detailed solution is waiting for you! 
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment