Overview of basic types and reference types
The values of data types in JS include: base type values and reference type values
Basic data type: undefined;null;boolean;number;string
Reference type value: In memory, JS does not allow direct access to memory location, so action references rather than actual objects
Second, how to detect data types
1. Detection of basic data types: Using typeof
var s = "AAA";
Alert (typeof s); return string
2. Reference type (object type) Detection: Using instanceof
Alert (person instanceof Object);
Alert (person instanceof Array);
Alert (person instanceof Regexp);
3. Special case: instanceof Detection object always returns TRUE, and always returns false when detecting the base type (because the base type is not an object)
Returns a function when the typeof is detected, and returns object when detecting the regular expression
Three, the difference between the basic type and the reference type
1. Reference types can add properties, the base type cannot be
2. When copying, the basic type is to copy directly a new variable, the old and new variables are not related;
The reference type also duplicates the new variable, but the variable is a pointer to the same object as the old two pointers
3. Parameter Pass of function: all the parameter transfer principle is to pass the external variable to the function's parameter by the way of copying. Therefore, the operation of the parameter inside the function has no effect on the external original variable
The following are examples of the basic types and reference types that are validated as arguments:
function Addten (num) {
num = ten;
return num;
}
var count =;
var result = Addten (count);
This internal operation on NUM does not affect the value of external count
function SetName (obj) {
obj.name = "Nicholas";
obj = new Object ();
Obj.name = "Greg";
}
var person = new Object ();
SetName (person);
alert (person.name); Returns "Nicholas" that describes the name of the person object that is not affected externally
The above is a small series to introduce the basic types of JavaScript and reference types (recommended), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!