First, basic type and value of reference type
1. values for basic types and reference types
Primitive Type value: Refers to the simple data stored in the stack memory , that is, this value is completely stored in the memory of a location, they occupy a fixed amount of space.
Reference type value: Refers to those objects stored in the heap memory , these types of real data is stored in the heap memory, while in the stack memory is only a pointer , this pointer to the object in the heap memory of an address .
Basic types of replication : The amount of space that a basic type occupies in memory is fixed, and when it is copied, it is re-opened to a space in the stack memory, accessed by value.
replication of reference types: Because the size of this object is not fixed, it is placed in the heap memory, but the memory address size is fixed, so the stack is stored in the memory of the object in the heap address. In this case, when the reference is found, the address is removed from the stack memory, and then the corresponding value is located in the heap memory , which is the reference access. Copy is the value of the copied stack memory, that is, a reference to the copy, Two variables point to the heap in memory objects or the same object.
2. Copy the value of a variable
Basic type: Copy is the value of the variable itself, after copying is in the stack space re-open a space, after the completion of the copy of the two variables are independent of each other, it is irrelevant, so the value of one variable will not affect the value of another variable.
Reference type: Copy is only the address of the copied stack space , and the heap memory is the same object , that is, the copy of the reference, the result is two variables point to the same memory block, so, When one of the two variables changes the value of the object, the result of another variable's output is changed because they are pointing to the same object in memory.
3. Passing Parameters
In js, parameter passing is a value pass, there is no reference pass.
value type: Pass the value of the variable itself, and copy is the same, the function changes the value of the variable, does not affect the source variable value
reference type: The same is the value pass , which passes the address value in the variable stack memory space, and if the value of one of the object's properties is changed in the function, the value in the source variable also changes because in heap memory they are pointing to the same object.
function func (num) { num.name=123;} var box={};box.name= ' ABCD '; alert (box.name); // ABCD func (box); alert (box.name); // The 123 value has been changed in the function func.
4. Detection Type
Detection of the type of the variable can be implemented with TypeOf, but this is only a few of the basic data types returned, it is not so useful when detecting the type of object, because Null,object,array,regexp will return object, That way, you don't know what type of variable it is.
You can use instanceof to determine that a variable is a specific reference type.
var box = [+ +]
instanceof Object); //true instanceof Array); Console.log (Array.isarray (box)) //true// returns true if this type otherwise returns false
II. implementation Environment and scope
The execution environment, also known as scope, is a very important concept in many programming languages, stipulating that variables or functions have access to other data, and that they specify their own behavior.
The global execution Environment is the outermost execution environment, in a Web browser, the global execution environment is considered a Window object, so all global variables and functions are created with the properties and methods of the Window object.
each function has its own execution environment , which destroys the execution environment and destroys variables and functions inside the execution environment when the execution of the code is complete. (The global execution environment needs to be destroyed only when the Web page is closed or when the application exits.) )
When code executes in an environment, a scope chain of variable objects is created, and the purpose of the scope chain is to ensure orderly access to all variables and functions that the execution environment has access to.
The function body also contains functions, only this function can access the inner layer of the function. The variables of the intrinsic function can be accessed through the scope chain to the variables of the external function, and the scope chain can be searched upward to query the variables. But it can't be reversed.
No block-level scopes
block-level scopes represent blocks of code segments that have braces closed , such as if, and therefore support conditional judgments to define variables. Variables defined in code blocks such as If,for are accessible outside the curly braces, which differs greatly from other languages.
if (true) { var color = ' red ';} Console.log (color); // Red
The difference of the var keyword in the function
When declaring a variable in a function, if you do not add the keyword var that variable will be considered global, the function outside can also access it, of course, before the access to execute a function first, Plus is local .
It is best not to initialize the variable without using the var keyword, because it can cause various errors, and the keyword VAR must be added to all initialization variables.
The general determination of a variable is determined by the search, now the scope of the current level, if not, in the upper scope to find, and so on, so access to local variables is more efficient than accessing global variables. Because there is no need to pick up the chain of scope
3. memory-related
JS also has a garbage collection mechanism, we do not need to worry about memory leaks, garbage collection mechanism will automatically manage the allocation of memory and garbage collection of useless memory.
The most common way of garbage collection in JS is to Mark Clear , which is to add a tag to all the variables in memory at run time, and then remove the tag of the variable being used in the environment, and the variable that is not tagged will be considered a variable to be deleted . Finally, the garbage collector completes the work of memory cleanup, destroys those tagged variables, and reclaims the memory space they occupy.
The garbage collector is run periodically , not running at any time, so you may experience some performance problems, but in general you do not need to worry about this issue.
In General, make sure that the page consumes less memory to make the page better performance, the best way to reduce memory consumption is that once the variable or object is no longer used, it is assigned a value of NULL, namely:box=null; to release the reference, which is called deleting the reference, using most of the global variables and global objects in this way.
JavaScript variables, scopes, and memory issues