There are several types of data in JS:
Base type: number Boolean String undefined null Symbol
Reference type: Object Function
The basic type of data is stored in the stack memory, while the reference type of data is stored in the heap memory
Copy variable values
Basic type:
var p = 1;var P1 = p;
Execution Result:
In other words, the basic type of replication is to create a new storage area in the stack memory to store the new variable, which has its own value, just like the previous value, so if one of the values changes, it will not affect the other.
Reference type:
var object1 = new Object (); var object2 = Object1;
Object2 copied the value of Object1, but the result is not the same as the basic type copy
Execution Result:
Defines an object that actually stores a pointer in the stack memory that points to the storage address of the object in the heap memory. The process of copying to another object actually copies the address of the object to another object variable, and two pointers point to the same object, so if one is modified, the other changes.
Object.name = ' Jhon '; alert (object.name); Jhon
There are no properties and methods for the basic type of data, but the reference type will have it, but why is the string type so many methods?
var string = "AAA"; var string2 = string.substring (0)); Aaa
The basic type does not have methods and properties, because once the property is created or the method is immediately destroyed, but in order to facilitate the programmer of this kind of basic type of data more convenient operation, at the bottom of the work done, actually this code equivalent:
var string = new String ("AAA"), var string2 = string.substring (0); string = null;
(1) Create an instance of string type (an instance of a reference type created with the new operator, which is kept in memory until the execution flow leaves the current scope). Objects that are automatically created by the basic wrapper type exist only in one
The execution of the line code is instantaneous and is immediately destroyed)
(2) Invoking the specified method on the instance
(3) Destroying the instance
After these three steps, the string value becomes the same as the object, as is the case in the Boolean number
The three types of String Boolean number are also called basic wrapper types
js--reference types and base types