First, introduction
ECMAScript contains two different types of values: primitive type values and reference type values. A primitive type value refers to a simple data segment; a reference type value refers to an object that consists of multiple values. When we assign a variable to a variable, the first thing the parser does is to verify that the value is a base type value or a reference type value. (JavaScript Advanced Programming (3rd edition))
Second, basic type value and reference type value
(1) Basic type value
Includes undefined, Null, Boolean, number, and string. These 5 basic data types are accessed by value, manipulating the actual values that are saved in the variable.
Example:
var aa = 10;var bb =aa;console.log (AA); 10console.log (BB); 10bb=11;console.log (AA); 10console.log (BB); 11
The above example, BB Gets the value is a copy of the AA value, although the value of the two variables is equal, but two variables save a copy of the basic data type value, completely independent. Therefore, when the value of BB is changed, the value of AA is still 10;
Demonstrate:
(2) Reference type value
Includes function, Object, Array. Copy of a reference type variable: Copies the pointer stored in the stack, copies the pointer to the space in the stack where the new variable is allocated, and the pointer copy and the original pointer execute the same object stored in the heap.
Example:
The object is a heap that is stored in memory. In the above example, B is a copy of a, and A and B is actually stored in the stack address, pointing to the address of the array object, after the copy operation, A, b two variables will actually refer to the same object, so changing one of them will affect the other.
Demonstrate:
Third, type detection
typeof can detect Undefined, Null, Boolean, number, string, and object. But it is not possible to detect whether it is an array, then there is a detection method instanceof.
var s = ' Hello '; var n =1;var u;var b =true;var nu=null;var o =new object (); var a = new Array (); Console.log (typeof s); Stringconsole.log (typeof N); Numberconsole.log (typeof u); Undefineconsole.log (typeof B); Booleanconsole.log (typeof Nu); Objectconsole.log (typeof O); Objectconsole.log (typeof a); Objectconsole.log (o instanceof Object); True Console.log (a instanceof Array); True Console.log (o instanceof RegExp); False
Iv. issues
So how do you back up the data for the reference type? I can't find the answer to the question of changing the array into a basic type.
JavaScript value types and reference types