The difference between primitive data types and reference data types in JavaScript
1. Basic data types and reference data types
ECMAScript includes two different types of values: primitive data types and reference data types.
Basic data types refer to simple data segments, and reference data types refer to objects that consist of multiple values.
When we assign a variable to a variable, the first thing the parser needs to confirm is whether the value is a primitive type or a reference type.
2. Common basic data types:
Number, String, Boolean, Null, and Undefined. Primitive data types are accessed by value because the actual value stored in the variable can be manipulated directly. Example:
var a = 10;
var b = a;
b = 20;
console.log(a); // 10 value
Above, b obtains a copy of a worth, although the values of the two variables are equal, but the two variables hold two different basic data type values.
b just holds a copy of a's copy. Therefore, a change in b has no effect on a.
The following diagram illustrates the process of assigning this basic data type:
3. Reference type data:
That is, the object type Object type, such as: Object, Array, Function, Data, etc.
JavaScript's reference data types are objects that are stored in heap memory.
Unlike other languages, you cannot directly access locations in the heap memory space and manipulate the heap memory space. Only the reference address of the object in the stack memory can be manipulated.
Therefore, the reference type data stored in the stack memory is actually the reference address of the object in the heap memory. Through this reference address, you can quickly find the object in the saved heap memory.
var obj1 = new Object();
var obj2 = obj1;
obj2.name = "I have a name";
console.log(obj1.name); // I have a name
Indicates that the two reference data types point to the same heap memory object. obj1 is assigned to onj2. In fact, the reference address of the heap memory object in the stack memory is copied to obj2.
但是实际上他们共同指向了同一个堆内存对象。实际上改变的是堆内存对象。
下面我们来演示这个引用数据类型赋值过程:
4、总结区别
a 声明变量时不同的内存分配:
1)原始值:存储在栈(stack)中的简单数据段,也就是说,它们的值直接存储在变量访问的位置。
这是因为这些原始类型占据的空间是固定的,所以可将他们存储在较小的内存区域 – 栈中。这样存储便于迅速查寻变量的值。
2)引用值:存储在堆(heap)中的对象,也就是说,存储在变量处的值是一个指针(point),指向存储对象的内存地址。
这是因为:引用值的大小会改变,所以不能把它放在栈中,否则会降低变量查寻的速度。相反,放在变量的栈空间中的值是该对象存储在堆中的地址。
地址的大小是固定的,所以把它存储在栈中对变量性能无任何负面影响。
b 不同的内存分配机制也带来了不同的访问机制1、基本数据类型和引用数据类型
ECMAScript包括两个不同类型的值:基本数据类型和引用数据类型。
基本数据类型指的是简单的数据段,引用数据类型指的是有多个值构成的对象。
当我们把变量赋值给一个变量时,解析器首先要确认的就是这个值是基本类型值还是引用类型值。
2、常见的基本数据类型:
Number、String 、Boolean、Null和Undefined。基本数据类型是按值访问的,因为可以直接操作保存在变量中的实际值。示例:
var a = 10;
var b = a;
b = 20;
console.log(a); // 10值
上面,b获取的是a值得一份拷贝,虽然,两个变量的值相等,但是两个变量保存了两个不同的基本数据类型值。
b只是保存了a复制的一个副本。所以,b的改变,对a没有影响。
下图演示了这种基本数据类型赋值的过程:
3、引用类型数据:
也就是对象类型Object type,比如:Object 、Array 、Function 、Data等。
javascript的引用数据类型是保存在堆内存中的对象。
与其他语言的不同是,你不可以直接访问堆内存空间中的位置和操作堆内存空间。只能操作对象在栈内存中的引用地址。
所以,引用类型数据在栈内存中保存的实际上是对象在堆内存中的引用地址。通过这个引用地址可以快速查找到保存中堆内存中的对象。
var obj1 = new Object();
var obj2 = obj1;
obj2.name = "我有名字了";
console.log(obj1.name); // 我有名字了
说明这两个引用数据类型指向了同一个堆内存对象。obj1赋值给onj2,实际上这个堆内存对象在栈内存的引用地址复制了一份给了obj2,
但是实际上他们共同指向了同一个堆内存对象。实际上改变的是堆内存对象。
下面我们来演示这个引用数据类型赋值过程:
4、总结区别
a 声明变量时不同的内存分配:
1)原始值:存储在栈(stack)中的简单数据段,也就是说,它们的值直接存储在变量访问的位置。
这是因为这些原始类型占据的空间是固定的,所以可将他们存储在较小的内存区域 – 栈中。这样存储便于迅速查寻变量的值。
2)引用值:存储在堆(heap)中的对象,也就是说,存储在变量处的值是一个指针(point),指向存储对象的内存地址。
这是因为:引用值的大小会改变,所以不能把它放在栈中,否则会降低变量查寻的速度。相反,放在变量的栈空间中的值是该对象存储在堆中的地址。
地址的大小是固定的,所以把它存储在栈中对变量性能无任何负面影响。
b 不同的内存分配机制也带来了不同的访问机制