js basis - value types and reference types

In ECMAScript, the value of the variable may be present in two types, i.e., the original value and the reference value.

  Original value Reference value
concept The original value refers to the  primitive type  value, also called  basic types Reference value refers to  a reference type value (class)
type of data Number、Stirng、Boolean、Null、Underfined Object、Function、Array、Date、RegExp 
storage Stack (Stack) , accounting for fixed memory space, destroyed after use

Heap (heap) , total memory space is not fixed, not necessarily be destroyed after use, only one object without any reference to time,

Garbage collection system will not recover destroyed

Assignment way
1. copy values , create a new object
2. Save the copy of the value itself
3. duplicate data in memory is completely independent
1. copy references , create a new reference
2. Save the replication is a pointer pointing object
3. The variable assignment of addresses stored in a separate storage,
Modifying one of the objects in the two variables, the time to access a further reference will also have access to the modified value.
4. Use the new () method is to construct an object reference type
Whether the value of variable Immutable variable
Scope Function scope, when the internal function changes to take effect, destroyed when the function fails Modify the function is the value of the data area in the runtime is modified, even if the function is destroyed, the value of the variable still be changed.
Compare the way

The comparison value

Compare cited
   
Only comparison value == →   
=== → only compare values, further comparison data type
 
 
  Detection Type   typeof operator   instanceof operator

Detailed stored
original values:

  var a =100;
  var b = a;
  a = 200;
  console.log(b);//100   

Variable object before replication

 

Variable object after copying

 

 

 

Reference values:

  var a = {age:20};
  var b = a;
  b.age = 21;
  console.log(a.age);//21   

 

  

 

Guess you like

Origin www.cnblogs.com/lingXie/p/11493620.html