In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to understand the alternative scope of JS". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to understand the alternative scope of JS"!
1. Scope of variables
As anyone with a little programming background knows, there are two scopes of variables: global variables and local variables.
Javascript is a weakly typed language. All variable declarations are received through var, such as
Var num = 1; var str = "string"; var flag = true
It seems to be a very convenient mechanism, but there are also times when people have a headache, some implicit type conversions often confuse them, do not expand here, and we can discuss them separately later. First look at global and local variables:
Var g = "global"; function f () {var l = "local";}
Note: 1. If the var declaration is removed from the function f (), the variable l is promoted from a local variable to a global variable.
two。 Local variables take precedence over global variables with the same name. If a local variable is also declared as g in the function f (), the global variable will be overwritten by the local variable.
two。 Scope and declaration in advance
Seeing the Javascript scope can be said to subvert my previous understanding of the scope. In programming languages like Java and C, the code in curly braces "{}" has its own scope, and outside this scope, these variables are not visible, which we call block-level scope.
But this does not apply to Javascript at all, because Javascript does not have block-level scope, but Javascript has functional scope. In short, the function scope is that variables are defined in the body of the function that declares them and any function nested within the body of the function.
An extended understanding of the statement that variables are defined in the body of the function that declares them and any function nested within that body: variables are available before they are declared. We call this feature declaration advance, meaning that all variables in the function are "advanced" to the top of the function body.
Let's look at a classic trap case:
Var v = "yoyo"; (function () {console.log (v); var v = "check now"; console.log (v);}) ()
There is nothing special about the result of the second execution "check now", why the output of the * is not "yoyo" but "undefined".
The explanation of this problem uses the above sentence, the local variable is always defined in the whole function body, that is, the local variable covers the global variable of the same name in the function body, and the local variable is really assigned only when the program executes the var statement. So, at this point, you'll probably understand why it's undefined, because you haven't encountered var yet, that is, there's no definition, which is equivalent to the following form:
Var v = "yoyo"; (function () {var scope; console.log (v); var v = "check now"; console.log (v);}) (); question? ? ? Modify the above code slightly to: var v = "yoyo"; (function () {console.log (v);}) ()
The running result is:
Compared to the above code, there is only one less line to add a local variable v and assign a value, but the result is "yoyo".
The reason why the output of "yoyo" here, can not follow the above stereotype of thinking. There is a saying above that "local variables are always defined in the whole function body", but there is no definition of local variables here, so according to the scope chain to be mentioned below, we will look for variables layer by layer, and * find the global variable v, so the output of * is "yoyo".
The above is my personal understanding, if you have your own understanding of these two situations, please give it below, please do not hesitate to comment.
3. Domain chain
Global variables are always defined in the program, and local variables are always defined in the function in which it is declared and within its nested functions.
Each piece of Javascript code (global code or function) has a chain of scopes associated with it, which is a list or linked list of objects. For example, when Javascript needs to find the value of the variable x, it will start with * * objects in the chain. If the object has an attribute named x, it will use it directly. If there is no attribute named x, it will continue to look for the next object in the chain until it is found. If it is not found on the entire chain, the attribute x is not considered to exist. For example:
Name= "lwy"; function t () {var name= "tlwy"; function s () {var name= "slwy"; console.log (name);} function ss () {console.log (name);} s (); ss ();} t ()
At this point, I believe you have a deeper understanding of "how to understand the alternative scope of JS". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.