In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of es6 scope, which is very detailed and has certain reference value. Friends who are interested must finish it!
Scope?
One of the most basic models of almost all programming languages is the ability to store values in variables and take out the ability to modify them. in fact, the ability to store and extract values in variables gives the program state. Without this concept, a program can perform some tasks, but they will be greatly limited and will not be very interesting. But where should these variables be stored and how can they be read? In order to achieve this goal, we need to make some rules, which are: scope
[recommended for related courses: JavaScript video tutorial]
What are the main scopes?
Scope is mainly divided into global scope, function scope, dynamic scope and block scope.
Object Typ
Window global scope
Fn function scope
{} block level scope
This dynamic scope
Scope lookup rules?
1. The execution environment can be divided into global scope and function scope.
2. every time you enter a new execution environment, a scope chain is created to search for variables and functions.
3. The local environment of the function can access not only the variables within the scope of the function, but also the parent environment and even the global environment.
4. The global environment can only access globally declared variables and functions, but cannot directly access any data heavy in the local environment.
Global scope
Variables are defined outside the function, that is, global variables.
Global variables have a global scope: all scripts and functions in a web page can be used.
If the variable is not declared within the function (without using the var keyword), it is a global variable.
/ / in the following example, carName is within the function, but it is a global variable.
/ / the carName variable can be called here
FunctionmyFunction () {
CarName= "Volvo"
/ / the carName variable can be called here
}
Variables that are not defined within the function or in the code block exist as attributes of window/global, variables that are not defined with var can be delete, while global variables cannot.
Function scope
A variable declared within a function is called a function scope. The variables inside the function cannot be externally
Direct access, which can be accessed through retun or closure.
/ / the carName variable cannot be called here
FunctionmyFunction () {
LetcarName= "Volvo"
/ / the carName variable can be called within the function
}
FunctionmyFunction () {
LetcarName= "Volvo"
RerurncarName
/ / the carName variable can be called within the function
}
Letfn=myFunction () / / Volvo
FunctionmyFunction () {
LetcarName= "Volvo"
FunctiongetName () {
Console.log (carName)
}
ReturngetName ()
/ / the carName variable can be called within the function
}
MyFunction () / / Volvo
Block level scope
After the emergence of es6, the block-level scope is added with the let command, and the outer scope can not get the inner scope, which is very safe and clear. Even if the outer and inner layers use the same variable name, they do not interfere with each other.
About the temporary dead zone
* the variable is not available until the let command is used to declare it. If you call this situation, it is called a temporary dead zone.
Let characteristics
* there is no variable promotion in let
* let does not allow repeated declarations
* have block-level scope
* you cannot use window to call
Const characteristics
* define constants
* it is not allowed to modify the value of a constant
* declare before assignment is not allowed
* ditto
If (1) {
Leta=1
Console.log (a)
}
Dynamic scope
Dynamic scopes do not care about how functions and scopes are declared and where they are declared, only where they are called. The mechanism is very similar to that of this.
In fact, there is a lexical scope extending from js (static scope), while the opposite of lexical scope is dynamic scope, and the scope of a function is determined when the function is called.
Letname='youzi'
FunctiongetName () {
Letname= "tuzi"
FunctiongetAge () {
Console.log (name)
}
ReturngetAge ()
}
GetName () / / tuzi
Lexical scope
JavaScript uses lexical scope, and the scope of a function is determined when the function is defined.
Letname='youzi'
FunctiongetName () {
Console.log (name)
}
FunctiongetAge () {
Letname='tuzi'
GetName ()
}
GetAge () / / youzi
The above is all the content of the article "sample Analysis of es6 scope". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.