Scope scope in JavaScript introduction to _javascript techniques

Source: Internet
Author: User
In JavaScript, the scope of a variable is divided by function-the variable is valid within the scope of a function. Like what:
Copy Code code as follows:

var f = false;
if (true) {var f = true;}//This time F is within the IF, that is, within the block, equivalent to or globally scoped
Alert (f)//So the result is true


Again, the following example:
Copy Code code as follows:

var f = false;
function Test () {
var F = true; This is a variable defined within a function that is valid inside a function and is released when the function has finished executing
}
Test ();
Alert (f)//result is false and does not change because of the execution of Test ()



Again, a global variable declared in JavaScript can be considered a property of a Window object, such as:
Copy Code code as follows:

var test = "This is a test";
Alert (window.test = = test)//result is true

This just validates that the global variable is also the property of the Window object.

Finally, let's look at
[Code]
function Test () {
F = false;
}
Test ();
Alert (f)//result is False
[HTML]

Then, if you do not add var (implicit declaration) when declaring a variable, it is also considered a global variable, although it is defined within the function.

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.