Stack and heap

Memory: is the running space of the computer memory particle: memory is divided into independent space, space has independent regions, the smallest of which is called memory particle memory particle is divided into two regions stack heap,

Default stack/heap relationship: one-to-one, many-to-one (addresses in two stacks correspond to the space of a heap) The stack:

It holds the address of the object and passes the value of the variable when the function is called.

—- space is small, stable, there are all addresses can not be modified

After the advanced

Js main thread function execution is pressed hereCopy the code

Heap: the storage is the value, the data space is large, the value inside so it can be modified. First in first out

Garbage collection is check hereCopy the code
Stack var a = 10 var b = a b = 20 console.log(a) //10 console.log(b) //20 // basic type (value transfer data) // copy is the value // except for objects and functionsCopy the code
Heap var o ={name:'admin'} var O2 = oo2. name = 'root' console.log(o) //root console.log(o2) //root // complex type (reference type, // Copy the address, the same address, pointing to the same value // object and functionCopy the code

Data type classification:

Value passing :(base data)(stack) in memory, the value is the address, the address is the value and the copy is the value (var)

Reference passing :(complex data)(heap) in memory, the address is the address, the value is the value copy is the address (exists in the stack obj address)

The reference data type stores a pointer on the stack to the starting address of the entity in the heap. When the interpreter looks for a reference value, it first retrieves its address in the stack and then retrieves the entity from the heapCopy the code

Depth copy

1. Difference between shallow and deep copies

The deep copy (stack) copies the value. After copying the basic data, modifying the copied data will not affect the original data.

Shallow copy (heap) of complex data, then copy the address, may cause multiple Pointers to the same object, complex data copy, modify the copied data, will affect the original data, because the same pointer to the data is modified.

A way to achieve deep copy

(…). Implementing deep copy

let obj={ name:'zs', age:18 } let obj1 = {... obj} obj.age = 12 console.log(obj) //name:'zs',age:18 console.log(obj1) //name:'zs',age:12Copy the code

(JSON) Deep copy

Parse (obj2) let obj={name:'zs', age:18} let obj2 = json.stringify (obj) obj2 = json.parse (obj2) Age = 12 console.log(obj) //name:'zs',age:18 console.log(obj2) //name:'zs',age:12Copy the code

(for-in traversal) implement deep copy

let obj={
    name:'zs',
    age:18
}
let obj3 = {}
for(let attr in obj){
    obj3[sttr] = obj[attr]
}
obj.age = 12
console.log(obj)   //name:'zs',age:18
console.log(obj3)  //name:'zs',age:12
Copy the code

(Object.assign) Implements deep copy

Let arr =[1,2,3,4,5] let arr2 =[] object.assign (arr,arr2) // merge two arrays arr.push(8) console.log(arr) // The console. The log (arr2) / /,2,3,4,5,8 [1]Copy the code

Macro and micro tasks

When JS is running code, it opens up two Spaces in the running memory, one is called macro task, the other is called micro task

Macro task:

Synchronous program, executed in macro task, setInterval() setTimeout whole code Script timer method executed in macro task, timer callback function executed in micro task,

Micro tasks:

The Promise as a whole is handled in the microtask where the nextTick asynchrony mechanism is executed.

P: If two JS files are executed, wait until the macro and micro in the first JS file is completed before executing the next JS file,

Execution sequence: macro tasks are executed first, that is, synchronous. For example, setInterval suspends macro tasks, continues macro tasks, and then executes microtasks (setInterval’s callback functions are also microtasks).

Prototype chain inheritance

An object is an instance and any function that is used by new is a constructor

Explanation:

Constructors use the new operator to generate an instance. Constructors are also functions that have a prototype property, which is the prototype object

Define that each instantiated object has a private attribute (called __proto__) pointing to its constructor’s prototype object. The prototype object also has a prototype object of its own (__proto__), cascading up until an object’s prototype object is null. Join point of prototype chain: __proto__

__proto__===null; undefined object.prototype. __proto__== null; undefined object.prototype. __proto__== null