1. Overview of basic types and reference types
The values of data types in js include: basic type values and reference type values
Basic data types: undefined;null;boolean;number;string
Reference type value: saved in memory, js does not allow direct access to memory locations, so when operating references instead of actual objects
2. How to detect data types
1. Detection of basic data types: use typeof
var s = "AAA"; alert(typeof s); //Return to string
2. Reference type (object type) detection: use instanceof
alert(person instanceof Object); alert(person instanceof Array); alert(person instanceof Regexp);
3. Special case: instanceof always returns true when detecting object, and always returns false when detecting basic types (because the basic types are not objects)
Typeof returns Function when detecting function, and Object when detecting regular expressions.
3. The difference between basic types and reference types
1. You can add attributes to the reference type, but not the basic type.
2. When copying, the basic type directly copies a new variable, and there is no relationship between the new and old variables;
The reference type also copies the new variable, but this variable is a pointer, and the old and new pointers point to the same object
3. Function parameter transfer: The principle of all parameter transfer is to pass external variables to the function's parameters through copying. Therefore, the operation of the internal function on the parameters has no effect on the external original variable
The following are the following to verify the parameters as basic types and reference types as examples:
function addTen(num){ num += 10; return num; } var count = 20; var result = addTen(count); //The internal operation on num here will not affect the value of the external count function setName(obj){ obj.name = "Nicholas"; obj = new Object(); obj.name = "Greg"; } var person = new Object(); setName(person); alert(person.name); //Return "Nicholas", indicating that the name of the external person object is still not affectedThe above is what the editor introduced to you and talks about the basic types and reference types in Javascript (recommended). I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!