JavaScript Learning Notes 3 Scope _ Basics

Source: Internet
Author: User
In JavaScript, the global environment itself is an object. This object is window in the browser host, and when JavaScript is used in other non-browser hosts, such as an embedded environment, it may be another object.
It also corrects the idea that JavaScript is used only in browsers, and that JavaScript can be used in many non-web situations, and that JavaScript is doing well in a number of embedded applications, Of course, I've just heard the legend.
Now, when we write down: Var i=1, it's actually a variable that declares a window scope.
And when we write down the I=1, we declare the properties of a window.
Look at this piece of code:
<script type= "Text/javascript" > var a = "Hello"; b = "World"; Test (); function Test () {alert (A + "" + B); var a = "Welcome"; b = "the"; Alert (A + "" + B); Alert (A + "" + B); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The result of this code output is: undefined world,welcome, hello.
We will explain separately:
In the previous section, we said that when JavaScript is precompiled, all VAR variables are created and the default value is undefined, where we can give an example:
We can write a piece of code like this:
<script type= "Text/javascript" > Alert (a); alert (b); var a = "111"; b = "111"; </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

When we run this script, we can find that the first pop-up undefined, and then back to the prompt script error, hint B does not exist. This shows that a is already created and initialized to undefined during precompilation, whereas B can only be interpreted sequentially in the actual runtime. In fact, pre-compiled JavaScript code can be nearly understood as follows:
<script type= "Text/javascript" > var a = undefined; alert (a); alert (b); A = "111"; b = "111"; </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Next we can talk about the scope of the function, whenever the code runs into a function, the JavaScript engine automatically creates a new scope, then takes the new scope as a child of the current scope, and then switches the current code scope to this new scope. When the code exits the function, the scope is destroyed, and the scope of the code is returned to his parent scope.
Well, it's almost ready, and then we'll explain the first question: ask what will output undefined world.
First the code is precompiled, when the test method is entered, a new scope is opened, and the global scope is used as his parent scope. The method in test is then precompiled, as in the previous code, and the test method is precompiled after the method body is roughly as follows:
Copy Code code as follows:

function Test () {
var a = undefined;
Alert (A + "" + B);
var a = "Welcome";
b = "the";
Alert (A + "" + B);
}

Of course, B cannot be found under the current scope, so he will go to his parent domain, the global scope, to find b= "world." And that's how it happens.
The second pop-up welcome, there is nothing to say.
The third time, pop hello We can understand that Var A is just a local variable of method test, and B is not declared in advance, so he goes to the parent scope to find the corresponding definition.
OK, next, let's take a look at a few variants of this method.
<script type= "Text/javascript" > var a = "Hello"; b = "World"; Test (); function Test () {alert (A + "" + B); A = "welcome"; b = "the"; Alert (A + "" + B); Alert (A + "" + B); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

First, we change Var A in the method body to a, we don't look at the answers, we analyze them, first of all, in the precompilation phase, there's almost no change in the body of the method, so at this point A and B are going to go to their parent scopes, so the first result should be Hello World, The second time nothing said: Welcome, the third time because A and B are not defined in this scope, they are changing the values within the parent scope, so you should export welcome.
We continue:
<script type= "Text/javascript" > var a = "Hello"; b = "World"; Test (); function Test () {alert (A + "" + B); var a = "Welcome"; var B = "the"; Alert (A + "" + B); Alert (A + "" + B); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

As with the above analysis, the undefined undefined,welcome China,hello world should be exported.
Go on:
<script type= "Text/javascript" > A = "Hello"; b = "World"; Test (); function Test () {alert (A + "" + B); var a = "Welcome"; b = "the"; Alert (A + "" + B); Alert (A + "" + B); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

It should be undefined world,welcome china,hello.
After the test, there is no problem, do not know you understand?
So we can conclude that each variable, when it cannot find its own definition, looks up through the chain of action, so that unpredictable errors are likely to occur, and many difficulties are added to the error. Even more troubling, the variable values on the parent scope may be modified, so we should try to add var when declaring the variable, although JavaScript does not force us to do so.
Related Article

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.