Usually, the names used in a section of program code are not always valid or available, and the code scope that limits the availability of a name is called the scope scope
. When a method or member is declared, it has the current execution context context
environment, and within a specific value context
, expressions are visible and can be referenced. If a variable or other expression is not in the current scope, it cannot be used. Scopes can also be hierarchically layered based on the code level, so that a child scope can access a parent scope. It usually refers to searching along the chain of scope and not being able to reference variables and references from a child scope in the parent scope.
JavaScript
scope is static scope static scope
, also known as lexical scope lexical scope
. Its main feature is that, when encountered in a function scope, if it is neither a parameter nor a locally defined variable in the function, it looks up in the context when the function is defined. In contrast, dynamic scope dynamic scope
is different. When encountered in a function scope, if it is neither a parameter nor a locally defined variable in the function, it looks up in the context when the function is called.
Calling s()
prints a
as 1
, which is static scope, meaning that the scope is determined at declaration time. In the case of dynamic scope, 2
would be printed here. Nowadays, most languages use static scope, such as C
, C++
, Java
, PHP
, Python
, etc., while languages with dynamic scope include Emacs Lisp
, Common Lisp
, Perl
, etc.
Variables or methods declared directly at the top level run in the global scope. The scope can be viewed by using the function's [[Scopes]]
property, which holds the function's scope chain and is an internal property of the function that cannot be directly accessed but can be printed to view.
After declaring a function, the running environment for methods or members declared within the function is the function's function scope.
If there is let
or const
within a code block, the block will form a closed scope for the variables declared with these commands from the beginning of the block.
You can see the scope chain of s
through [[Scopes]]
. When we use d
in s
, if the parameter or local variable d
does not exist in s
, it will look up in the [[Scopes]]
. It finds the value when it reaches the Block
scope, and it gets the value of d
. The same principle applies when using c
, b
, and a
, where it looks up the scope chain to localContext2
, localContext
, and Global
scope, respectively. In summary, when a function or variable needs to be used and the value is not found in the current scope, it will look up in the parent scope until it reaches the global scope. This chain formed by the lookup process is called a scope chain.