JavaScript variables can be used to hold two types of values: the base type and the reference type. The basic types include the following 5 types: Undefined, Null, string, Boolean, number, and so on, the reference type is object, and the base type and reference type have several features:
1, the basic type value in memory occupies a fixed size of space, so the value of the base type is stored in the stack memory;
2. Copy the value of the base type from one variable to another, and a copy of the value is created.
3, the value of the reference type is an object, stored in the heap;
4. A variable containing a value of a reference type actually does not contain the object itself, but rather a pointer to that object;
5. Copy the value of the reference type from one variable to another, and the copy is actually a pointer, so two variables always refer to the same object;
6, determine a value is which basic type can use the TypeOf operator, and determine a value is which type of reference can be used instanceof operator ';
varNum1=5;varNum2=num1;num2=6; num2;//6NUM1;//5varobj1=NewObject ();varObj2=Obj1;obj1.name= "DD"; alert (obj2.name);//"DD" points to the same object; the same address nvarobj1=NewObject ();varObj2=NewObject ();//two objects, the address is not the same
varS= "DDD";varb=ture;varC=22;varu;varn=NULL;var0=New function(); Alert (typeofs);//string;Alerttypeofb);//Boolean;Alerttypeofu);//undefined;Alerttypeofn);//object;Alerttypeof0);//object;
When determining what type of reference is used, use the instanceof
Alert (Persons Instanceif object); // is the variable person an object? alert (colorss instanceif array); // is the variable colors an array? alert (pattern Instanceif RegExp); // is the variable pattern a regexp?
Preprocessing (elevation) of functions
alert (a); var a=123; // The result of implementation is undefined; var A; alert (a); a= 123;
The first step in the execution of the function is to preprocess and then step through the code to see the instance
<script type= "Text/javascript" >alert (a); varA = 1; alert (a); functionA () {alert (123);} alert (a); A ();</script>First step pre-compilationvarA; functionA () {alert (123);} The second step is to execute the code: alert (a);//function A () {alert (123);}A=1; alert (a);//1Alert (a)://1A ();//error, equivalent to 1 ();
JS does not follow the function of reference, should not be passed reference parameters as by reference
function setName (obj) { obj.name= "dddd";} var person=New object (); SetName (person); alert (person,name) ; // DDD Here is a mass-transfer.
The scope of the sibling function is not nested and cannot be accessed
function F1 () { var b=2; alert (b); F2 ();} function F2 () { var d=3; alert (d); alert (b); // does not appear b}
But if this is the nested function F2 () nested in the F1 () scope, the F1 () scope is nested in the global scope, forming a scope chain;
function F1 () { var b=2; alert (b); function F2 () { var d=3; alert (d); alert (b); // 2 }}
The FOR statement and if statements have no enclosing scope functionality, and are global
All variables exist in an execution environment (also known as scope), which determines the life cycle of the variable and which part of the code can access the code, as summarized below:
1, the execution environment has a global execution environment and function execution environment points;
2, every time into a new execution environment, will create a search for variables and functions of the scope chain;
3, the local environment of the function not only has access to the variables in the scope of the function, but also has access to its inclusion (parent class) environment and even the global environment;
4. The global environment intelligently accesses the variables and functions defined in the global environment, and does not directly access any data in the local environment;
5, the execution environment of the variable can help to determine how to free memory;
The JavaScript language is a programming language with an automatic garbage collection mechanism, and developers do not have to care about memory allocation and recovery issues: JavaScript garbage collection routines can be summarized as follows;
1. The value of the departure scope is automatically marked as recyclable and therefore will be deleted during garbage collection;
2, "Mark Clear" is the current popular garbage collection algorithm, the idea is to add a tag to the value that is not currently used, and then recycle it
3, the release of the reference to the variable can not only help to eliminate the circular reference object, but also for garbage collection benefits, in order to ensure the effective recovery of memory, should be in time to remove the unused global objects, global object properties and circular reference variable reference;
function Createperson (name) { var localperson=New Object (); Localperson.name=name; return Localperson;} var Dd=createperson ("dddd");dd=null; // Manual release of the DD reference
Javascrip Learning notes for advanced programming scope and execution environment