In js, there are two types of values: the original values (values and primitive types) and a reference value (reference type)
// 随机生成n到m的随机数
function random(n,m){
return Math.floor(Math.random() * (m - n + 1) + n);
}
调用时,输入实参取代n,m
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、 Symbol(es6新增) | 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, when only one object does not have any references, 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 |
== comparison value only === only comparative value, the data type is relatively
|
||
Detection Type | typeof operator | instanceof operator |