I. Overview of basic types and reference types
Values for data types in JS include: base type values and reference type values
Base data type: undefined;null;boolean;number;string
Reference type value: In memory, JS does not allow direct access to memory locations, so when manipulating references instead of actual objects
Second, how to detect the data type
1. Detection of basic data types: Using typeof
var s = "AAA"; alert (typeof s);
' 2. Reference type ( object type) detection: Using instanceof
Alert (person instanceof Object), alert (person instanceof Array), alert (person instanceof Regexp);
3. Special case: Instanceof detects that object always returns True, and always returns false when detecting a base type (because the base type is not an object)
typeof return function when detecting functions, returning an object when detecting regular expressions
Three, the difference between the basic type and the reference type
1. Reference types can add attributes, and basic types cannot be
2. When copying, the basic type is directly copied a new variable, the new and old two variables are not related;
The reference type also copies the new variable, but the variable is a pointer to the new and old two pointers to the same object
3. Parameter passing of the function: all parameter passing principle is the argument that the external variable is passed to the function by means of copying. Therefore, the operation of parameters inside the function has no effect on the external original variable
The following are examples of parameters as basic types and reference types, respectively:
function Addten (num) { num + = ten; return num;} var count = 20;var result = Addten (count);//The internal operation of NUM does not affect the value of external count function SetName (obj) { obj.name = "Nicholas"; C5/>obj = new Object (); Obj.name = "Greg";} var person = new Object (), setName (person), alert (person.name) ; Returns "Nicholas", stating that the name of the person object that still does not affect the external
Basic types and reference types in JavaScript