Home > Web Front-end > JS Tutorial > body text

Javascript variable scope two small features that may be ignored_javascript tips

WBOY
Release: 2016-05-16 18:31:34
Original
724 people have browsed it

Maybe some experts already know it, but I think these two things still have some value, so I’ll share them with you here
. There is the following code:

Copy code The code is as follows:



What will be output when calling this function? You may think that it will pop up undefined, but in fact, it will pop up the string "an url", which involves the concept of variable scope in JavaScript.
In JS, each scope has a corresponding "variable object" (it may not be accurate to call it this way, just know that there is such a thing), which stores the identifier defined by the current scope. When the JS program starts, it will enter the global scope. In our program, because the test function is defined in the global scope, the "variable object" here stores the identifier of the test function. Next, we called the test function and entered the scope of the test function. The scope of the test function itself also has its own "variable object". When entering the scope of test, the scope stored in the global scope will also be The identifier is copied in, so the identifier defined in the global scope can be accessed in the test function, but the inner content cannot be seen in the outer scope. If this is the case, then our alert(temp) here will output undefined, because temp is defined in the inner scope. But why can we find identifiers defined in the inner scope here?
Because the "variable object" of the scope in the with statement block is read-only, the identifier defined in its current layer cannot be stored in this layer, but is stored in its upper-level scope, that is, The scope of the test function, so our alert(temp) statement can access the temp variable defined internally. The same situation occurs in the try catch statement.
. There is no concept of statement blocks in Javascript. The code is as follows:
Copy code The code is as follows:

if (true) {
var temp = "oh";
}
alert(temp);

Very simple lines of code, if you follow the usual Based on our programming language experience, we would think that the alert statement will report an error, but this is not the case. The alert statement normally outputs the string "oh", so there is no concept of statement block in
Javascript.
The above two examples are very simple. They may not be very technical. Many experts should have already understood them. However, these two language features should still be quite useful. Maybe after understanding these features, you will be able to write JS less frequently. There are some opportunities for errors, so I still post them to share with you, hoping to help everyone when writing JS.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!