Javascript function scope learning example (js scope)

Source: Internet
Author: User

In some C-language programming languages, each piece of code in curly brackets has its own scope, and variables are invisible outside of the code segment that declares them, block scope, which is called block scope, does not exist in javascript. Instead, javascript uses function scope: variables are defined in the declared function body and any function nested in the function body. In the following code, I, j, and k defined in different locations are all defined in the same scope.

Copy codeThe Code is as follows:
Function text (o)
{
Var I = 0;
Alert (typeof o );
If (typeof o = "string ")
{
Var j = 0;
For (var k = 0; k <10; k ++)
{
Alert (k); // output 0-9
}
Alert (k); // output 10
}
Alert (j); // output 0
}

The function scope of javascript indicates that all the variables declared within the function are always visible in the function body. Interestingly, this means that the variable is even available before it is declared. This feature of javascript is informal referred to as hoisting. That is, all variables declared in the javascript function body (not involving assignments) are "advanced" to the top of the function body. See the following code

Copy codeThe Code is as follows:
Var global = "globas ";
Function globals ()
{
Alert (global); // undefined
Var global = "hello QDao ";
Alert (global); // hello QDao
}

Due to the function scope feature, local variables are always defined throughout the function body, that is, the variables in the function body cover the global variables with the same name. Even so, when the program executes the var statement, the local variable will be assigned a value. Therefore, the above process is equivalent: the variable declaration in the function is "advanced" to the top of the function body, and the colleague variable initialization stays at the original position:

Copy codeThe Code is as follows:
Var global = "globas ";
Function globals ()
{

Var global;
Alert (global); // undefined
Global = "hello QDao ";
Alert (global); // hello QDao
}

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.