This wave of energy to kill Jane book reading notes

Source: Internet
Author: User
Tags garbage collection


Memory space management data type structure execution context
Variable object active object Scope scope chain closure


Before we learn about memory space, we need to have an intuitive understanding of three of data structures. They are heap (heap), stack (stack), and queues (queue). After the execution context of JavaScript is generated, a special object called a variable object is created
The underlying data types are simple data segments, with 5 of the underlying data types in JavaScript, namely undefined, Null, Boolean, number, String. The underlying data types are accessed by value, because we can directly manipulate the actual values that are saved in the variable.
Unlike other languages, JS refers to data types, such as array arrays, whose values are not fixed in size. The value of the reference data type is the object that is saved in the heap memory
JavaScript does not allow direct access to the location in the heap memory, so we cannot manipulate the object's heap memory space directly. When you manipulate an object, you are actually manipulating the object's reference rather than the actual object.
JavaScript has an automatic garbage collection mechanism, so what is the rationale for this automated garbage collection mechanism? It's very simple to find out which values are not going to be used, and then release the memory they occupy. The garbage collector performs a release operation every fixed period of time. In JavaScript, the most common is to find which objects are no longer used in the local scope by means of the algorithm that marks the purge, and when the function completes, the local variable is not necessary, so the garbage collector can easily make a judgment and recycle it. But it's hard to judge when global variables need to automatically free up memory space, so in our development, we need to avoid using global variables as much as possible. Each time the controller goes to the executable code, it goes into an execution context. The execution context can be understood as the execution environment for the current code, which forms a scope. Global Environment, function environment
So in a JavaScript program, there is bound to be multiple execution contexts, as mentioned in my last article, where JavaScript engines deal with them in stacks, which we call the function call stack. The bottom of the stack is always the global context, and the top of the stack is the currently executing context.
The execution context can be understood as the environment in which the function executes, and each function executes, creating such an execution environment for the corresponding function.
Global context out of stack after browser window closes
The lifecycle of the execution context, the creation phase: Creating a Variable object, establishing a scope chain, and determining this point to the execution phase: variable assignment, function reference, other code invocation activation, starts creating the corresponding execution context, in the process of execution context generation, the variable object, the scope chain, And the value of this will be determined separately. We already know that when a function is invoked (activated), a new execution context is created. The lifecycle of an execution context can be divided into two phases.
Creation phase in this phase, the execution context creates the variable object, establishes the scope chain, and determines the point of this.
Once the code execution phase is created, the execution of the code begins, at which point the variable assignment, function reference, and other code are executed.
The creation of variable objects, followed by several procedures. Create a arguments object.     Checks the parameters in the current context to establish the properties and property values under the object. Checks the function declaration of the current context, which is the function declared using the functions keyword. In a variable object, establish a property with the function name, which is a reference to the memory address where the function resides.      If the property of the function name already exists, the property will be overwritten by the new reference. Check the variable declaration in the current context, each found a variable declaration, in the variable object in the variable name to establish a property, the property value is undefined. If the property of the variable name already exists, to prevent a function with the same name from being modified to undefined, it is skipped and the original property value is not modified. The scope chain, which consists of a series of variable objects in the current environment and the upper environment, guarantees the orderly access of the current execution environment to the variables and functions that meet the access rights. The scope chain is made up of a series of variable objects, in which we can query the identifiers in the variable object so that we can access the variables in the previous scope.
Many people misunderstand that the current scope and upper scope are inclusive, but they are not. The one-direction channel with the most front-end as the starting point and the end point is more apt to describe it. All of the ends are global variable objects.
After entering the execution phase, the variable object is converted to the active object, the attributes inside can be accessed, and then the operation of the execution phase begins.
If you are asked the difference between the variable object and the active object in the interview, then you can answer it freely, they are all the same object, but in the different lifecycle of the execution context. However, only variable objects that are in the execution context at the top of the function call stack become active objects.
The global context has a special place, its variable object, the Window object. This particular, also applies to the this point, and this also points to window. In JavaScript, we can define scopes as a set of rules that govern how the engine finds variables in the current scope and nested child scopes based on variable names or function names, and only global scope and function scopes exist. The scope and execution context are two concepts that are completely different. I know a lot of people will confuse them, but be sure to distinguish them carefully. The entire execution process of JavaScript code is divided into two phases, code compile phase and code execution phase. The compile phase is done by the compiler, which translates the code into executable code, which is determined by the scope rules at this stage. The execution phase is done by the engine, and the primary task is to execute executable code, which the execution context creates at this stage. One is determined at compile time and one is created during the execution phase

Original address:

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: 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.