First let’s look at the data types in JS (a lot of crap! But the explanation should be quite clear!

Number String Boolean Undefined null Object Bigint Symbol

The basic value types are: null, and undefined, number, Boolean, string, symbol

The reference type can be object

Common reference types are array and function

Before we get into these two types, let’s generalize about what language types are strong and weak and dynamic and static. So what is strong typing? Weakly typed languages? What is dynamic? Static? language

A static language is one in which the type of a variable needs to be confirmed before use

Languages that need to check data types during runtime are called dynamic languages

The operation of stealthily converting – implicit conversion. Languages that support implicit conversion are called weakly typed languages

In using these data types we also need to know where the storage space of these types is (divided into stack space and heap space)

Value type:

function foo(){ var a = 1 var b = a a = 2 console.log(a); //2 console.log(b); //1 } foo()Copy the code

Foo () and a=1(we’ll ignore the precompilation process), then we have data in the stack:

In b=a, b refers to the value of A

And then a is equal to 2, so I change the value of a to 2

The output simply looks for the values of A and B in the stack!

Reference type:

Function foo() {var a = 'login' var b = a c = {name: 'htable'}Copy the code

Foo () and then a=login(we’ll ignore the precompilation process), and we’ll have data in the stack:

In b=a, b refers to the value of A

C = {name: ‘htable’}; c = {name: ‘htable’}

Finally, d=c, where D holds the value of C on the stack

Look at one more reference type code

Function bar(){var c = {name:' console.log 'var d = c.name=' console.log '; // console.log(d.name); } foo()Copy the code

The other steps will be pushed by you: Let’s look at c.name=’ big liu ‘and change the value of c in the heap to big Liu, unchanged on the stack

So the final output values are all old Liu

Summarize value types and reference types

  1. Object types are stored in heap space, and only the reference address of the object is kept in stack space
  2. A value assignment copies the value of a variable exactly, whereas a reference type assignment copies the address of the reference

Let’s consider a question: Why did we introduce a heap structure to store data?

You should all know that the stack structure is characterized by “come in, come out”, so when we put all the types on the stack, it affects the speed of context switching. If you go out with a girl watching movies (you bought the ticket in advance, but first to dinner) girl power is small, don’t ask how much, I put all her things in his bag, bag to imagine a stack, put all things into the bag, when you go to the movies, to vote, have to get into the bag over (assuming a lot) when you find the ticket. Did you lose a girlfriend when the movie was over?

In summary:

  1. Js engine needs to use the stack to maintain the context state during program execution. If the stack is too large, all data will be placed in the stack, which will affect the efficiency of context switching
  2. The downside of heap storage is that it takes time to allocate and reclaim memory