Two small features that may be ignored in the Javascript variable scope

Source: Internet
Author: User
Tags try catch variable scope

Some experts may already know about these two things, but I think these two things still have some value. So I 'd like to share them with you here.
. There are the following Code : Copy code The Code is as follows: <SCRIPT type = "text/JavaScript">
Function Test (){
With (location ){
VaR temp = "an URL ";
}
Alert (temp );
}
Test ();
</SCRIPT>

what will be output when this function is called? It may be thought that it will pop up undefined, but in fact, it pops up the "an URL" string, which involves a concept of variable scope in JavaScript.
in JS, each scope has a corresponding "variable object" (this is not necessarily accurate, so it is enough to know that there is such a thing ), the identifier of the current scope definition is stored here. When the JS Program starts, it enters the global scope. In our program, because the test function is defined in the global scope, so the "variable object" Here stores the identifier of the test function. Next, we call the test function and enter the scope of the test function. The scope of the test function also has its own "variable object". When we enter the scope of the test function, the identifier stored in the global scope is also copied. Therefore, the identifier defined in the global scope can be accessed in the test function, but in the outer scope, cannot see the content of the inner layer. In this case, the alert (temp) here will output undefined, because temp is defined in the internal scope. But why can I find the identifier defined in the inner-layer scope here?
because the "variable object" in the with statement block is read-only, the identifiers defined in this layer cannot be stored in this layer, instead, it is stored in the previous scope, that is, the scope of the test function. Therefore, our alert (temp) statement can access the temp variable defined in the internal layer. The same situation occurs in try catch statements.
. javaScript does not contain block statements. The Code is as follows: copy Code the code is as follows: if (true) {
var temp =" oh ";
}< br> alert (temp);

a few simple lines of code. If we use the common programming language experience, we will think the alert statement will report an error. However, this is not the case, the alert statement outputs the "oh" string normally, so the
JavaScript does not have the concept of statement block.
the two examples above are very simple. Maybe they are not very high-tech and many experts should have already understood them. However, these two language features should be useful. Maybe, after learning about these features, when writing JS, there will be fewer errors, so I 'd like to share it with you, hoping to help you write JavaScript.

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.