The stack

A place where goods are stored or where passengers can stay

A stack in a data structure

Stack is a set of data storage mode, is characterized by the first out, last in first out, some people are popular said to eat the principle of vomit, finally eat the latest swallow out, look at the general picture

Code examples

function one() {
  function two() {
      function three() {
          debugger;
      }
      three();
  }
  two();
}
one();
Copy the code

(one()-two()-three()) (one() -three()) (one() -three()) (one())

Memory area

  • A stack is also an area of memory where data is stored
  • While the program is running, it needs memory space to store data. Generally, systems divide memory into two different types: one called a stack, and the other called a heap
  • The stack is structured so that each block is placed in a certain order and you know exactly how big each block is
  • The heap has no structure and data can be stored anywhere. As a result, stack addresses faster than heap

Data that is local and occupies a certain amount of space is generally stored in the stack, otherwise in the heap, and all objects are stored in the heap

function task() {
    var a = 1;
    var b = 2;
    var c = {
        name: 'zhufeng',
        age: 10
    }
}
task();

Copy the code

Examples A and B are placed on the stack, and object C is placed on the heap.

The queue

A queue is a linear table with limited operations

What makes it special is that it only allows delete operations at the front end of the table and insert operations at the back end

The end that performs the insert operation is called the tail of the queue, and the end that performs the delete operation is called the head of the queue

Because the queue can only be inserted at one end and deleted at the other end, only the earliest elements in the queue can be deleted from the queue first. Therefore, queues are also called fifO linear tables. There is also a popular saying: eat the law of pull, the first to eat out

Execute the text (the focus of this section)

  • When a function runs, it creates an Execution environment called the Execution Context.
  • The execution context creates an Object called a Value Object, where the underlying data types are stored. To illustrate, the execution context is not only about variable objects, but also about many objects such as: this.
  • Values that refer to data types are stored in the heap, and we manipulate objects by manipulating their reference addresses

Code sample

function task(){ var a = 1; Var b = {name:'zhufei'} var c = [1,2,3]; }Copy the code

So you can see here that a is one and it’s stored on the stack, and B and C are objects so it’s stored in memoryThe point below is that the current execution context, the varobject, contains the variables that are required to execute the function, and nothing else

Data types (make up a common interview question)

There are seven basic data types in JS

  • Boolean Null Undefined Number String Symbol
  • Object {} [] /^$/ new Date() Math