Javascript variable scope Two a small feature that may be ignored _javascript tips

Source: Internet
Author: User
Tags try catch variable scope
Maybe some experts already know, but I think these two things still have some value, so get here and share it with you
. Have the following code:
Copy Code code as follows:

<script type= "Text/javascript" >
function Test () {
With (location) {
var temp = "an url";
}
Alert (temp);
}
Test ();
</script>

What does it output when calling this function? It might be thought that it would pop undefined, but in fact it pops up with the "an URL" string, which involves a concept of variable scopes in JavaScript.
JS, each scope has a corresponding "variable object" (so called not necessarily accurate, know that there is a thing on the line), which holds the current scope definition identifier. JS program at the beginning, will enter the global scope, in our program, because the global scope defined the test function, so it here's "variable object" is stored in the test function identifier. Next, we call the test function and we go to the scope of the test function and the scope of the test function itself has its own "variable object", which, when it enters the scope of the test, also copies the identifiers stored in the global scope, so The identifier defined in the global scope can be accessed in the test function, but the contents of the inner layer cannot be seen in the outer scope. If so, then our alert (temp) will output undefined because temp is defined in an inline scope. But why is it possible to find identifiers defined in an inner scope?
Because the scope "variable object" in the With statement block is read-only, so the identifier defined on his level cannot be stored in this layer, but stored in its upper-level scope, which is the scope of the test function, so our alert (temp) statement You can access the TEMP variable defined in the inner layer. The same situation also appears inside the try Catch statement.
. JavaScript does not have a statement block concept, the code is as follows:
Copy Code code as follows:

if (true) {
var temp = "Oh";
}
Alert (temp);

Very simple lines of code, if the usual program language experience, we will think that the alert statement will be an error, but this is not the case, the alert statement normally output "Oh" This string, so the
The concept of a statement block does not exist in JavaScript.
The above two examples are simple, perhaps not very high technology, many experts should have already known, but these two language features should still be more useful, perhaps to understand these characteristics, when writing JS will reduce some of the chance of error, so still send up and share with you, hope to be able to write JS when you help.
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.