The concepts of data, memory, variables and their relationships

What is data

Data is something stored in memory that represents specific information, which is essentially made up of lots of zeros and ones

Characteristics of data

Can be passed

var a = 123
var b = 123
Copy the code

Can operation

var a = 123
a += 1
Copy the code

What is memory

Memory is the space created by memory sticks in computer hardware that can be used to store data when they are powered on. This space is temporary (space disappears after a power failure and data is lost).

Memory creation and death (lifecycle)

The memory module is powered on. Memory space that can store data is generated. Memory space that can process data is powered offCopy the code

== Each memory block has a unique memory address value ==

Stack memory

Store global variables, local variables and other basic types of data

Heap memory

Store various objects

How does the JS engine manage memory

Memory creation and death (lifecycle)

The memory module is powered on. Memory space that can store data is generated. Memory space that can process data is powered offCopy the code

== Details of memory release ==

  • Global variables: generated when a variable is declared and released after the browser environment is closed
  • Local variables: function calls are generated, released automatically after the function calls are completed and collected by the garbage collector
  • Object type data: Objects need to become garbage objects and then be collected by the garbage collector at some point (very soon)

The automatic freeing of memory does not occur at the same time as the garbage collector collection mechanism, even though the difference between the two is negligible

What is a variable

Variables store variable values. Each variable corresponds to a piece of memory (with memory address). Variable names serve as identifiers to find the corresponding memory, and variable values are the data stored in memory

When a variable is of a common type

var a = 123
console.log(a)
- console.log(a) reads the value of the variable named A stored in the stack, which is 123 */
Copy the code

When a variable is a reference type

// obj holds data of a reference type, so obj is a reference type variable
var obj = {
    name: 'Fitz'
}
console.log(obj)
/* If (*) - a block of memory is created in stack memory to hold the variable name obj. - a block of memory is created in heap memory to hold the object. The object holds the value 'Fitz' -console. log(obj) reads the value of the variable name obj in stack memory, except that the value is the address of the block of memory in which the object is held in heap memory, rather than the value of the normal type variable */

var obj2 = obj
Obj2 */ assigns the value of the variable named obj to obj2 */
Copy the code

The relationship between data, memory, and variables

  • Memory is the space used to store data
  • The variable is the memory identification, used to find the corresponding memory space, to achieve the purpose of reading data

When passing arguments to a function, whether they are passed by value or by reference

Understanding 1: both are value passes, that is, the value of a variable is passed as an argument to a function parameter

var a = 1
var obj = {name: 'Fitz'}
function func1 (a) {
    a = a + 1
}
function func2 (obj) {
    console.log(obj.name)
    obj = {name: 'Lx'}
    console.log(obj.name)
}

func1(a)
console.log(a)  / / 1
func2(obj)              // 'Fitz' 'Lx'
console.log(obj.name)   // 'Fitz'
When a function is called to pass an argument, the value of the variable (a, obj) is passed to the parameter of the function, except that the value of the variable is divided into two types: 1. 2. The variable value of the object type (reference type) is the memory address value (the object is in heap memory) */

/* console.log(obj.name) is still the reason for Fitz: - Assign the value of obj (memory address value) as an argument to the func2 parameter (obj) - internal execution statement obj = {name: 'Lx'} - emphasis: At this point, the obj outside the function (global) still points to {name: 'Fitz'} while the obj inside the function (local) points to {name: 'Lx'} - after the function completes, the parameter's obj(local obj) becomes a garbage object ready to be processed by the garbage processor */
Copy the code

Understanding 2: there is both value passing and reference passing, that is, value passing when the value of a variable is common type data, and reference passing when the value of a variable is object type data

var a = 1                   // The variable value is plain type data
var obj = {name: 'Fitz'}    // The variable value is object type data
function func1 (a) {
    a = a + 1
}
function func2 (obj) {
    console.log(obj.name)
    obj = {name: 'Lx'}
    console.log(obj.name)
}

func1(a)                // The external variable a(global a) is passed by value to the parameter A (local A)
console.log(a)  / / 1
func2(obj)              // 'Fitz' 'Lx' now passes the value (memory address value) of the external variable obj(global obj) to the parameter obj(local obj), which is called referential passing
console.log(obj.name)   // 'Fitz'
Copy the code