The difference between a value type and a reference type in JavaScript
a , not stored the same way
L Basic Data type
A variable stores a simple data segment, storing a specific value, and a lightweight way of storing data.
L Reference type
A value of a reference type, an object that can consist of multiple values, and a variable of a reference type stores an object reference address. Reference type is the weight of the data stored way, allocated in heap memory, frequently creating objects damaging performance.
Reference type (n multiple) Object, Array, Date, Function 、......
Second, Dynamic properties are not the same
L reference types can dynamically add properties
var person=new Object ();
Person.name= "Jack";
person.age=20;
Console.log ("Name:%s, Age:%d", person.name,person.age);
Results
Description
Property Dynamic Add Success
L value type cannot be added
var person= "";
Person.name= "Jack";
person.age=20;
Console.log ("Name:%s, Age:%d", person.name,person.age);
Results
Description
Property has failed to add dynamically, indicating that this is a variable of the value type.
three , Different ways to assign values
L value type, content of copy variable
var num=10;
var num2=num;//creates a new space and copies num values to num2.
The contents of this two variable are num2, and the content of the contents is a copy of Num. duplicate Parts of a similar operating system
var num=10;
var num2=num;
num2=100;//does not affect the value of num
Console.log ("num=%d num2=%d", num,num2);
Results
Demo Memory Evolution (Field operations)
L reference type, copy reference address
var person1=new Object ();
person1.age=18;
var person2=person1;//copy reference address, give variable person2, then point to an object
person2.age=30;//is actually the object that Person1 pointed to.
Console.log ("person1.age=%d person2.age=%d", person1.age,person2.age);
Results
Description
All two variables point to the same object, the object is to be referenced by the variable, and the number of references is unrestricted.
Memory changes drawing (Field Operation)