JavaScript function Scope Learning Example

Source: Internet
Author: User

There is no block-level scope in the

 javascript, and instead of JavaScript using function scopes, use the example below to learn how to use JS scopes

In some programming languages like C, each piece of code within the curly braces has its own scope, and the variables are invisible beyond the declaration of their code snippet, which we call block-level scopes, and no block-level scopes in JavaScript. Instead, JavaScript uses function scopes: variables are defined in the body of the function that declares it and in any function that is nested within the function body. In the following code, I,J and K, defined in different locations, are defined in the same scope   code as follows: function text (o)    {       var i=0; &nb sp;      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       }        alert (j);/Output 0   } The function scope of     JavaScript refers to all variables declared within a function that are always visible within the function body. Interestingly, this means that the variable is even available before it is declared. This feature of JavaScript is informally called Declaration advance (hoisting), which is that all variables declared in the function body of JavaScript (not involving assignment) are "advanced" to the top of the function body. See ToThe following code     code is as follows: Var global= "Globas";    function Globals ()    {       alert (global)//undefined      & nbsp var global= "Hello Qdao";        Alert (global)//hello Qdao   }     Because of the nature of the function scope, local variables are always defined throughout the function body, That is, the variable inside the function body obscures the global variable with the same name. However, when the program executes to the VAR statement, local variables are actually assigned, so the procedure above is equivalent to declaring the variable in the function "ahead" to the top of the function body, and leaving the colleague variable initialization at the original location:     Code as follows: Var global= " Globas ";    function Globals    {            var global;    &NB Sp   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.