In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what the Javascript scope problem is, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
In front-end learning, the question of scope has been widely raised. What is scope and what is scope chain? In Javascript, how to understand these concepts is the key to learn the language well, so in the process of learning front-end development, I need to learn and summarize the scope of javascript----.
Scope is not difficult, but few people can explain what a scope is, and it is obviously not enough to know what it is without knowing why, so let's go deep into the simple.
When it comes to scope, we have to talk about a noun-execution environment. What is the execution environment?
Execution environment is one of the most important concepts in Javascript. It defines that variables or functions have access to other data and determines their respective behavior. Then in each execution environment, there is an object called "variable object". All variables and functions in the execution environment are stored in this object. The object is inaccessible, and it can only be accessed by the javascript interpreter in the background.
When parsing Javascript code, the browser creates an execution environment for each function and generates a variable object in that execution environment to store variables and their internal functions. The window we often use is the outermost execution environment, also known as the global execution environment. After the code execution of each execution environment is completed, the environment will be destroyed and the variable objects in it will also be destroyed, while the global execution environment will only be destroyed when the browser or web page is closed, and the other execution environment (or internal) is the local execution environment (function).
Each function has its own execution environment, so when the execution flow of the code enters a function, the execution environment is pushed into an environment stack and popped up after the function execution is completed. the control of this execution environment is returned to the previous execution environment, and when the contemporary code is executed in an execution environment, the variable objects in that environment are connected to the scope chain. The function of the scope chain is to ensure orderly access to authorized variables and functions in the execution environment. At this point, you may be a little confused, so use some code and pictures to explain it.
/ / window scope var name0 = 'scope0'
Console.log (name0) / / name0 function scope1 () {can be accessed here
Var name1 = 'scope1'
Console.log (name0,name1) / / name0,name1 function scope2 () {var name2 = 'scope2' can be accessed here
Console.log (name0,name1,name2) / / name0,name1,name2 function scope3 () {var name3 = 'scope3' can be accessed here
Console.log (name0,name1,name2,name3) / / you can access name0,name1,name2,name3} here
When the JS interpreter executes this code, it generates four execution environments, namely window,scope1,scope2,scope3. Then, when the JS code is executed, each execution environment is pushed into the execution stack, and variable objects are generated and connected to the scope chain (from top to bottom). The scope chain generated by * is:
Window → scope1 → scope2 → scope3
For each variable object in the execution environment, its scope chain is the variable object that it originally added (for example, the scope chain of scope2 is scope2 and its predecessor scope1 and window). As we said earlier, the variable object in each execution environment is that the execution environment can access variables and functions. Personally, it is understood that this function is other variable objects on the scope chain of the variable object. Then it is easy to understand. Let's analyze what is on the variable object of the above code scope2, first of all, the parameter array (arguments, here is []) and the name2 variable. Then there are scope1 variable objects and global variable objects.
Having said so much and mentioned so many concepts and nouns, we seem to only talk about the scope chain, but not the scope, this is bullshit! All right, let's talk about scope now.
Let's start with the concept. Is that each execution environment can access other execution environments of his scope chain up through the scope chain, but not down. This is the scope.
Take the scope2 function, for example. He can access name2,name1,name0 but not name3. This is the scope limit, and he can only access the execution environment of scope1 and window (and itself). Well, for such a short period, I am drunk myself, but I think the key to the scope is the understanding of the execution environment, the object of the variable and the scope chain. These are the keys to a solid understanding of the scope of Javascript.
The above is what the Javascript scope problem is like. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.