JS variable small total

Classification variables:
1, stack memory (Stack) and heap memory (heap)
2, the basic types and reference types

# Stack memory (stack)
  is generally static allocation of memory, its memory allocation system automatically released.

# Heap memory (heap)
  is generally dynamically allocated memory, which is allocated memory, the system does not release, even if the program exits, that one was in there.

# Why is there stack memory and heap memory of the points
1, and garbage collection mechanisms. In order to minimize the memory occupied by the program is running;
2, when a method of execution, each method will establish its own stack memory, a variable defined within this method will one by one into this stack memory, with the method of execution is over destruction;
3, when we create an object in the program, this object will be saved to the run-time data area for repeated use, the runtime data area is the heap memory. Heap memory object does not end with the destruction process and even after the end of the method, the object can also be a reference to another (very common when passing parameters method) variable reference, the object still will not be destroyed, only when there is any reference variable references within a target, garbage collection system will not recover in time it verified;

* Five basic types:

  Number The, String, Null, Undefined, Boolean, these types of memory, respectively, in the space occupied by fixed size, their value is stored in the stack space, we are accessed by value;

* reference types:
  Array, Date, Math, RefExp, Function, etc.
ObjectObject(refer to objects stored in the heap memory); reference type, size is not a fixed value, the address stored in the stack memory pointer to a heap object.

# Brief Description:
  stack memory address stored in only access the object in the heap memory space allocated to this value. Since this value is not fixed, it can not save them to the stack memory. But the memory address size is fixed, it can be stored in the stack memory address memory. Thus, when a query reference type variable, memory address to start reading the stack, and then find the value of the heap through the address.

The basic difference between the reference types and the types of
  the actual value of the difference is the pass-by pass;

% pass Found:

% pass-:

& Shallow copy:

  shallow copy means copying only one object, when the object attribute is a reference type, the substance of which is a reference address to copy, modify any two objects point to a memory address value at the same time, other values are also will change; (when defining an object or array, often only variable storage address when we use a copy of the object, or if the property is an array of objects, then we pass the address and therefore only a child objects. when the property is accessed based on the address of the parent object back to the heap memory, when the parent and child objects associated with the occurrence of both the property value point to the same memory space;) &


deep copy:

  deep copy means copying objects all levels the object and its value are copied, the two objects modify any of these values will not change the value of another.

& Implementation of a deep copy:

1, achieved by a recursive method;
2, by JSON parsing achieved; JSON object into a string of sub-objects to JSON:the JSON.parse (the JSON.stringify (O));

Guess you like

Origin www.cnblogs.com/Angxf/p/10957037.html