In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shares with you the content of a sample analysis of the JavaScript scope. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Scope
Scope is simply the scope available after variables, functions, and objects are defined.
Console.log (a) {var axi1;} function test () {var baked 2;}
You can see that the variable b cannot be used outside. You can see that the scope can protect the data from being accessed and modified by the outside world. You can simply see that scopes can isolate variables from each other, that is, variables with the same name in different scopes will not conflict.
The most important and commonly used scopes are global scope and function scope. However, ES6 leads to a block-level scope because of the emergence of the let and const keywords.
Global scope
The global scope simply means that all domains have access to variables and method objects under the domain.
Var a = "global 1"; function test () {b = "implicitly converted to a global variable without var"; window.c= "directly regards variable c as global under window"; var d = "non-global scope" } # the first step is to execute test () test () # so that the variables in the method are defined and assigned # step 2: console.log (a) console.log (b) console.log (c) console.log (d)
Generally speaking, the properties of window are global variables, while the real form of window.c treats c as a property of window. Note that when declaring variables, do not take var, it is best to take var, so that it will not be promoted to global variables, resulting in mutual contamination of data.
Add that the test method is also a method in the global domain.
Function test () {var a = function () {console.log ("literal method")} b=function () {console.log ("method without var literal")} function test1 () {console.log ("normal declarative method")}}
This can be seen as a method of literal declaration, similar to one that can be seen as assigning a function to a variable and treating it as a variable. It has also been demonstrated in the previous pre-compilation.
Function scope
The function scope is the opposite of the global scope, it is not used everywhere, but in a certain scope, generally declared variables are only used within the function.
Function test () {var a = "non-global scope"; console.log (a)}
Now there is another problem, the global method can use variables within the scope of the function. So is it possible for a function to have a scope generated by the function below? And whether their variables can be used with each other?
Function test () {var a = "test method scope"; function test1 () {var b = "test1 method scope"; console.log ("value of a =", a);} # call function internal function test1 (); console.log ("value of b =", b);}
In this place, we can see that the scope is hierarchical, the inner scope can access the variables of the outer scope, and the external can not access the internal variables.
If,switch,for, while
Conditional statements and logical loops, * * they are neither functions nor like functions, nor do they create a new scope. * * variables defined by their blocks are retained in the scope where they exist.
Function test (a) {if (a > 1) {var bounded 13;} else {var bounded 1;} console.log (b);}
So when using conditional statements and logical loops, try not to use them in the global scope as much as possible. Because the variables in the body of the method affect other data.
Block scope
The emergence of a block scope generally depends on one of the two keywords let or const, otherwise the block scope would not exist.
Function test (a) {const b = "23"; if (a > 2) {const if----c---- 3 console.log ("first person if---c-", c)} if (a > 1) {console.log ("second person if----b----", b) console.log ("second person if----c----", c)}}
You can see that after the keywords let and const, the scope of the variable is within the pair of curly braces it declares. So the c variable in the first if cannot be obtained in the second if. Of course, obey: the inner scope can access the variables of the outer scope.
Learn about let and const. See the previous article: address.
Domain chain
This seemingly magical concept, simply said, is directly used within the scope, did not find a higher level, if not, find the overall situation will end.
Var a=1var b=3function test () {var axi2 console.log ("value of a", a); console.log ("value of b", b);}
Thank you for reading! This is the end of this article on "sample Analysis of JavaScript scope". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.