Javascript study Note 3 Scope

Source: Internet
Author: User

In Javascript, the global environment itself is an object. In the browser host, this object is a window, and when Javascript is used for other non-browser hosts, such as embedded environments, it may be other objects.
I have also corrected this idea. Many people think that Javascript is only used in browsers. In fact, Javascript can also be used in many non-Web scenarios, according to the introduction, Javascript has performed well in some embedded-based application fields. Of course, I have only heard of the legends.
To put it bluntly, when we write down: var I = 1, we actually declare a variable in the window scope.
When I = 1 is written, the attribute of a window is declared.
See the following code:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The output results of this Code are undefined world, welcome china, and hello china.
Let's explain separately:
As mentioned above, all var variables will be created during Javascript pre-compilation. The default value is undefined. Here we can give an example:
You can write the following code:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
When we run this script, we can find that undefined is displayed first, and a script error is returned, prompting that B does not exist. It can be indicated that a has been created and initialized as undefined during the pre-compilation process, while B can only be explained in order during actual running. In fact, the pre-compiled Javascript code can be almost understood as follows:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Next, let's talk about the Function scope. Whenever the code runs into a function, the Javascript engine automatically creates a new scope, then, the new scope is used as the subscope of the current scope, and the current Code scope is switched to this new scope. When the code exits the function, the scope is destroyed and the code scope is returned to its parent scope.
Well, the preparation is almost done. Next we will explain the first question: what will output undefined world.
First, the code is pre-compiled. When the Test method is started, a new scope is enabled, and the global scope is used as its parent scope. Then pre-compile the methods in Test. Like the above Code, the method body after the Test method is pre-compiled is roughly as follows:
Copy codeThe Code is as follows:
Function Test (){
Var a = undefined;
Alert (a + "" + B );
Var a = "welcome ";
B = "china ";
Alert (a + "" + B );
}

Of course, B cannot be found in the current scope, so B is found in its parent scope, that is, B = "world" in the global scope ". So this result is generated.
The second pop-up of welcome china is nothing to say.
The third time, hello china is displayed. As we can understand, var a is only a local variable of the Test method, and B will find the corresponding definition in the parent scope because it is not declared in advance.
Okay. Next, let's take a look at several variants of this method.

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
First, we will change var a in the method body to a. Let's analyze it directly without looking at the answer. First, in the pre-compilation stage, the method body has almost no changes, so at this time, a and B both go to their parent scope to search, so the first result should be hello world, and there is nothing to say for the second time: welcome china, for the third time, because neither a nor B is defined in this scope, the value in the parent scope is changed. Therefore, welcome china should be output.
We continue:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Like the above analysis, undefined, welcome china, hello world should be output.
Continue:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
It should be undefined world, welcome china, hello china.
It's okay after the test. Do you understand?
Therefore, we can conclude that every variable will be searched up along the chain when it cannot find its own definition. In this way, unexpected errors may occur, which makes troubleshooting difficult. Even more troublesome, the variable values in the parent scope may be modified. Therefore, we should add var whenever possible when declaring the variable, even though Javascript does not force us to do this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.