In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what are the knowledge points of es6 scope?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what are the knowledge points of es6 scope" article.
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
What are the main scopes?
Scope is mainly divided into global scope, function scope, dynamic scope and block scope.
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 not only access the variables within the function scope, but also include 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 function myFunction () {carName = "Volvo" can be called here; / / 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 accessed directly outside, and can be accessed through retun or closure.
/ / the carName variable function myFunction () {let carName = "Volvo" cannot be called here; / / the function callable carName variable} function myFunction () {let carName = "Volvo"; the rerurn carName; / / function callable carName variable} let fn=myFunction () / / Volvo;function myFunction () {let carName = "Volvo"; function getName () {console.log (carName)} return getName () / / 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 temporary dead zones * variables are not available until you declare them with the let command. If you call this situation, it is called a temporary dead zone. Let feature * let does not exist variable promotion * let does not allow repeated declaration * has block-level scope * cannot use window to call const feature * define constant * does not allow modification of constant value * does not allow declaration before assignment * ditto if (1) {let a = 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 (static scope) that extends js, while the lexical scope is dynamic, and the scope of a function is determined when the function is called.
Let name='youzi';function getName () {let name= "tuzi"; function getAge () {console.log (name)} return getAge ();} getName () / / tuzi
Lexical scope
JavaScript uses lexical scope, and the scope of a function is determined when the function is defined.
Let name='youzi';function getName () {console.log (name)} function getAge () {let name='tuzi'; getName ()} getAge () / / youzi is the content of this article on "what are the knowledge points of es6 scope?" I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about related knowledge, please 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.