One, the variables in JavaScript
The 1,javascript variable is loosely typed, which determines its essence: to save a particular value during a specific life cycle. Because there is no definition of what data type a variable must hold, its value and data type can change over the life cycle. ,
The 2,javascript variable has two values for the data type: the base type value and the reference type value. A reference type value is an object that consists of multiple values.
When assigning a value to a variable, the parser must first determine that this is a good reference type value for the basic data type.
Where 5 of the base data type is accessed by value, because the actual value saved in the variable can be manipulated, the value of the reference type holds the object in memory (that is, it cannot directly alter the specific meaning in the object, and must first be accessed to the object address.) The value of a reference type is accessed by reference. )
3, the property of the variable now: the basic data type, you can change the value, reference data type, you can change the property additions and deletions,
4, copy Variable value: (Assign to another variable)
Pass by value is made a copy, the operation on the copy is not related to the original variable; passing by reference refers to the same file, such as Obj1 copied to Obj2, then obj1 and obj2, changing the file, will affect each other's values.
。。。。。。。。
5, the variable is passed as a pass parameter (whether it is a basic data type or a reference data type)!!!! )
There is no doubt about the basic data type passing by value, but what about the reference type? It is said that the reference type is passed by value, more precisely the reference type is the address of the global reference type is passed by value. Then cut the rules by reference type and handle the!!!!!!!!!!
(If you create a new object inside a function, point to an external reference type, manipulate the new object without affecting the external reference type)
function SetName (obj) {obj.name= "Lipengffei"} var person=new Object (); SetName (person); alert (person.name);//"Lipengfei"
Instead of creating a new object in the function body, it creates an obj by value and points to person. A property is declared and assigned with this address. This will again be accessed by using person access, and when the end, obj disappears. But the contents of the person are saved.
function SetName (obj) {obj.name= "SDFSD"; obj=new object (); obj.name= "abc";} var person=new object (); SetName (person); alert (person.name);//sdfsd;
In this case, obj is declared again in the body of the function, thus cutting off the point of the original obj. This is not related to the person. The future operation is the respective operation of the respective,
6, Check type
To check if a variable is a basic data type, use the TypeOf
If it is a base data type, it returns the type of the base data type, and if it is null or an object, it returns an object (so that it cannot distinguish between what type of object)
The instanceof operator determines whether the object is of a reference type, returns TRUE or FALSE, for example: alert (person instaceof Array), and determines whether it is a group object.
Second, execution environment and scope
1, the execution environment, defines the variable or function has the right to access other data.
In a Web browser, the global environment is considered a Window object, so all global variables and functions are the properties and methods of the window.
After execution in an environment, its variables and functions are automatically destroyed.
2, scope chain.
Ensure an orderly access to the variables and functions that the execution environment has access to. The order of execution is from the front end of the scope, looking up sequentially. For example, a function will function in the body of a function first, affecting a variable outside, then the variable is looked up again to function.
3, extending the scope chain
A variable object is temporarily added to the front of the scope chain, and the variable object is removed after the code executes, in two cases:
The catch statement block for the Try-catch statement (creates a new variable object that contains the declaration of the thrown error object); With statement (adds a variable to the specified scope, such as URL and Location.url);
4, no block-level scope
Both the local variables in the {} are in the C language or the Java language, which are inaccessible externally, and are accessed by JavaScript. The {} is referred to here as the {} of the statement, not the local variable of the function. The local variables in the function are still not released to the external
The reason for this is mainly because there is no closed execution environment in the {} of the statement, they are still in the same execution environment, so they will be accessed. and other languages are not!!!!!
Three, garbage removal
JavaScript garbage collection is an auto-recycle mechanism, and the value of the variable leaving the scope is automatically marked and cleared automatically.
"Tag Cleanup" is currently the most mainstream garbage collection mechanism, the idea is to add tags to the currently unused values, and then recycle.
The idea of "reference counting" is to record the number of times that all values are referenced, and this algorithm is problematic!!!!! For example, a circular reference.
Lifting an object takes up space directly with NULL.
JavaScript variables, scopes, and memory issues