ECMAScript variables:
1. Basic type value (simple data segment)
2. Reference type value (object that may be composed of values) → Object saved in memory
------
Dynamic attributes: You can only dynamically add new attributes to referenced values for future use.
------
Copy variable values:
Copy the basic type value → Create a new value on the variable object → Copy to the new variable (no mutual influence)
Copy the reference type value → Copy the value stored in the variable object into the new variable allocation space (the copy is a pointer, pointing to the same object, affecting each other)
------
Pass parameters:
All functions in ECMAScript are passed by value.
(The value outside the function is copied to the parameters inside the function.
→ You can imagine the parameters of the ECMA function as local variables. )
Explanation of reasons:
Statement 1:
Function parameters are local variables, which are passed to function parameters on external values, and the internal and external values do not affect each other. The references and the duplicate addresses are addressed, so they have an influence on each other.
Statement 2: Object angle (parameter is an object obj)
------
Objects are passed by value → Parameter objects and external objects refer to the same object → The object pointed to by the external object has only one object in heap memory and is a global variable.
Don't mistake it as the result of the local object being reflected in the global scope (external) of the modified object → the local variables inside the function will be destroyed immediately after the function is executed.
------
Execution environment (sometimes referred to as "environment") and scope
The execution environment defines white or functions that have access to other data determine their respective behaviors → Each execution environment has a variable object associated with it → All variables and functions defined in the environment are stored in this object.
------
Each function has its own execution environment → When the execution flow enters a function → The function environment will be pushed into an environment stack → pop it up after the function is executed → Return control to the previous execution environment
When the code is executed in an environment, a scope chain of the variable object is created.
Scope chain purpose: Ensure orderly access of all variables and functions that have permission to access in the execution environment.
The front end of the scope chain is always a variable object in the environment where the code currently executed is located.
------
The internal environment can chain all external environments through scope chains, but the external environment cannot access the internal environment (the environment connection is linear, orderly → search the scope chain upward to query variables and function names)
Function parameters are also treated as variables
------
Extend the scope chain:
Some statements can temporarily add a variable to the front-end chain of scope (variables are removed after code execution)
1. The catch block of the try-catch statement
2.with statement
------
Javas has no block-level scope
Here we can simply distinguish between block-level scope and function scope
The above detailed explanation of JS variables and scope is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.