JavaScript reading notes (4)-variables, scopes, and memory issues

Source: Internet
Author: User

1, ECMAScript data types are divided into: basic type value and reference type value;

The parameters of all functions in ECMAScript are passed by value;

Check the type of object: Varible instanceof Constructor

Alert (person instanceof Object);

2. execution Environment and variables

Each execution environment has a variable object associated with it, and all variables and functions defined in the environment are stored in the object;

The global execution environment is the outermost of a specified environment, in a Web browser, the global execution environment is the Window object, all the global variables and functions are the properties and methods of the Window object, the code executes, the environment is destroyed;

Each function has its own execution environment, and when the execution flow enters a function, the environment of the function is pushed into an environment stack, and after the function executes, the stack ejects its environment and returns control to the previous execution environment;

When code executes in an environment, a scope chain of variable objects is created (scope chain). It guarantees an orderly access to all variables and functions that the execution environment has access to, the front end of the scope chain, and is always a variable object in the context of the currently executing code. If the environment is a function, its active object is used as a variable object, and the active object initially contains only one variable, the arguments object, the next variable object in the scope chain comes from the containing environment, and the next variable object is from the next containing environment, thus continuing to the global execution environment The variable object of the global execution environment is always the last object in the scope chain;

Identifier parsing is the process of searching identifiers at the level of the scope chain;

(1) extending the scope chain

Some statements can temporarily add a variable object to the front end of the scope chain, and the variable object is removed after the code executes;

Catch block for Try-catch statement: Creates a new variable object that contains the declaration of the thrown Error object;

With statement: The established object is added to the scope chain;

(2) no block level scope

Declaring variables, variables that use Var are automatically added to the closest environment, and the variables are automatically added to the global environment when they are initialized without using Var;

It is a common error to initialize a variable without declaring it, and in strict mode, initializing an undeclared variable results in an error;

Query identifiers, starting from the front end of the scope chain, querying the identifier matching the given name up and down, if found in a local environment, the search stops, if the variable is not found in the local environment, then continues to search up the scope chain, the search process will be traced back to the global environment of the variable object;

The cost of variable queries is that accessing local variables is faster than accessing global variables;

3. Garbage Collection

(1) JavaScript has an automatic garbage collection mechanism, and the execution environment is responsible for managing the memory used during code execution;

Principle: Identify variables that are no longer in use, and then release the memory they occupy, and the garbage collector periodically performs this operation at a fixed time interval (or a predetermined collection time in code execution);

(2) The garbage collector must keep track of which dry food is useful which variable is useless, marking a variable that is no longer useful for future recall of its occupied memory, the policy used to identify the useless variable is common in two ways: Mark clear and reference count;

(3) Mark Clear, when the variable enters the environment, the variable is marked as "Enter the environment", when the variable leaves the environment, it is marked as "leave the environment";

The garbage collector adds tags to all variables stored in memory at run time, and then it strips out variables in the environment and tags of variables referenced by variables in the environment, and variables that are tagged later will be treated as variables that are ready to be deleted, and variables in the environment cannot be accessed to these variables. Finally, the garbage collector completes the cleanup work, destroys those tagged values and reclaims the memory space they occupy; most browsers use this strategy;

(4) A reference count, which keeps track of the number of times each value is referenced, when a variable is declared and a reference type value is assigned to the variable, the number of references plus 1, conversely, if the variable that contains the value refers to another value, the number of references to that value is reduced by 1, and when the number of references becomes 0 o'clock, You can reclaim the memory space it occupies.

The problem is circular referencing: Object A has a pointer to object B, and object B also contains a reference to object A;

Some objects in IE are not native JavaScript objects, and objects in the BOM and Dom are implemented in the form of COM objects using C + +, and the garbage collection mechanism of COM objects uses reference counting policies. As long as the COM object is involved in IE, there is a circular reference problem.

Var Elemen=document.getelementbyid ("some_element");

Var myobject=new Object ();

Myobject=new Object ();

Element.someobject=myobject;

A circular reference is created between a DOM element in the code above and a native JavaScript object (myObject);

To avoid such problems, it is best to manually disconnect the native JavaScript objects from the DOM elements when they are not available;

Myobject.element=null;

Element.someobject=null;

(5) The garbage collector is running periodically, if the amount of memory allocated for the variable is very objective, then the recovery workload is quite large, it is very important to determine the time interval of garbage collection;

How the JavaScript engine's garbage collection routines work: variable allocations that trigger garbage collection, and critical values for literal and array elements are dynamic corrections;

The best way to manage memory and optimize memory consumption is to save only the necessary data for the code in execution, and if the data is no longer useful, it is best to set it to NULL to release its reference, which is actually de-referencing; this usage applies to the properties of most global variables and global objects;

JavaScript reading notes (4)-variables, scopes, and memory issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.