I. Explanation of nouns
The visualization of the noun helps to better understand the execution process. There are ECG, VO, AO, GO, execution context, execution environment stack, this, closure, prototype and prototype chain, Function and Object.
1. JS execution platform
Where can I execute JS code?
nodejs
- The browser
- Native and Non-native applications (
webview
)
So no matter where our JS code is executed, we ultimately need to provide an environment.
2. Execution environment (take browser platform as an example)
- Programs developed in programming languages will eventually be executed in memory when they run.
- in
JS
When the browser is loading the interface, it will actively request a piece of memory from the computer (execution environment stack). ESC
Execution stack context
3. Execution context (no need to visualize it)
Start with a piece of code that makes sense of the existence of an execution context:
const name = 'zce'
function foo() {
const name = 'lagou'
}
function fn() {
console.log('1111')}Copy the code
- a
JS
A file can contain multiple types of code (variables, functions, objects) that are combined to form code blocks. - Different code blocks may have the same name (as above)…… And so on, how does the code distinguish between them when it executes?
- How to distinguish: Put each piece of code in its own execution context
- We can think of the execution context as a kind of “container” that contains everything that is needed for the current code to be pushed and executed
EC
Execution context
4. Stack execution
- Execution environment stack, similar to large container (browser loading interface creates memory space by default), first in, then out
- Code execution (function calls) creates a separate execution context that goes into the environment stack and runs the internal code
- After the execution of the internal code is complete, consider whether to exit the stack
EC(G) global execution context
- By default, browsers generate a global execution context for storage.
- By default it automatically appears at the bottom of the stack (execution environment stack). When will the memory occupied by the execution context be freed? (Close the current page because the browser is multi-threaded and the current page is only one thread.)
VO(G) global variable object
- Global variable object (it is an object, must store data, store what?) .
VO(G)
inEC(G)
Global execution context.
7, GO global object
global object
The global object, and the global objectVO
“Variable object” is not the same thing- By default, the browser creates a space when loading the interface, which holds some files that can be accessed directly in JS
API
(setTimeout
,setInterval
,toString
……) - In order to
JS
Easy to find these built-in apis in the global execution contextVO
A variable calledwindow
(reference address) point to theseAPI
8, the statement
Such as: var name; const age
9, definitions,
Name = ‘aaa’
Second, the expression form of JS data type
- Base data type values are stored in stack space, and reference types need a separate space to store them (heap memory)
- The heap will have a memory address (hexadecimal address), and the variables on the stack will hold the memory address
- The scope chain lookup mechanism, which eventually leads to (
GO
) - The discussion of functions is generally divided into two parts: function creation + function execution
- The creation of a function is similar to that of a variable. The function name can be regarded as a variable, but the difference between it and a variable is that in the promotion stage (pre-parsing) the function is declared + defined, while the variable is only declared.
- A function is also an object and therefore occupies a heap of memory.
- For functions, the scope is defined at creation time (lexical scope, scope)
- The function body is stored as a string in the heap memory corresponding to the function.
- The purpose of a function execution is to retrieve the string code stored inside it and run it as real code.
- Because functions also contain a lot of data, each function creates a new, separate execution context each time it is called, in order to isolate data from other code blocks. Store its internal data in this context, and then perform the push call as a whole.
Third, the execution of the function
Function execution requires six steps:
- Determine the scope chain: the current execution context of a function (e.g.
EC(FOO)
The lexical scope of the functionEC(G)
) - determine
this
, such as:window
. (Follow-up discussion) - Initialize the
argument
Create a pseudo-array… - Parameter assignment: Parameter assignment is performed in
AO
Add a variable to the - Variable ascension
- Code execution: It is possible to open up new heap memory, assign values, create functions, and so on during code execution
Illustrate an example to understand function execution:
Closure mechanism
Closures are a mechanism where the code is just a concrete representation of what we call a large function nested within a small function and returned with a small function. Its main function is to protect and preserve data. Resolution: Function will produce new generated an execution context, generally the function code execution after requires a stack so as to release the memory space occupied by the current context, so as to release its internal statement and value, but if the data of the current execution context (generally are heap memory references) is outside of the current context variables referenced, The context cannot be released, and a closure is formed.
var zce = 100
function fn() {
var zce = 200
return function (a) {
console.log(a + zce++)
}
}
var foo = fn()
foo(10)
foo(20)
Copy the code
Diagram the execution of the above code:Analyzing closures in code: Closures have the advantage of being able to store some data, as shown belowzce
Inside the functionzce
And the globalzce
They don’t interfere with each other, and closures can also store data, for example0x001
The corresponding memory space should be inFN
It is released after execution, but becauseEC(G)
In the middle offoo
It has a reference to it, so it can continue to be used in subsequent code.
Closures and garbage collection
- As you can see from the above code execution, code execution requires memory space. Both stack and heap memory belong to the computer memory space
- There is a limit to the size of the memory space, so you can’t use it indefinitely, so you need memory management, which is garbage collection
Closure exercises
Analyze the execution of the following code:
var a = 10
function foo (a) {
return function (b) {
console.log(b + (++a))
}
}
var fn = foo(10)
fn(5)
foo(6) (7)
fn(20)
console.log(a)
Copy the code
Diagram stack execution of code: