JS scope and block level scope

Source: Internet
Author: User

Scope is always a priority in any programming language because it controls the visibility and life cycle of variables and parameters. Here, you first understand two concepts: block-level scope and function scope.

What is a block-level scope?

Any set of statements in a pair of curly braces ({and}) belongs to a block, and all variables defined in it are not visible outside the code block, which we call a block-level scope.

The function scope is well understood (*^__^*), and the parameters and variables defined in the function are not visible outside the function.

Most Class C languages have block-level scopes, but JS does not. Take a look at the demo below:

// C language   #include <stdio.h>  void  main ()  {     int i=2;     I--;      if (i)     {         int j=3;     }     printf ("%d/n", J);  }  

Running this code will cause "use of an undefined variable:j" error. As you can see, the C language has a block-level scope because J is defined in the IF statement block, so it is inaccessible outside the block.

And JS is how to behave, and then look at another demo:

Functin Test () {  for (var i=0;i<3;i++) {     }  alert (i);} Test ();

Run this code, pop up "3", visible, outside the block, the variable I defined in the block can still be accessed. In other words, JS does not support block-level scopes, it supports only function scopes, and variables defined anywhere in a function are visible anywhere in the function.

So how do we make JS have block-level scope? Remember, the variable defined in a function, when the function is called, the variable is destroyed, can we use this feature to simulate the block-level scope of JS? Look at the following demo:

function Test () {    (function  () {    for (var i=0;i<4;i++) {    }    })();    alert (i);  }  Test ();  

This time running again, will pop up "I" Undefined error, haha, realized it ~ ~ ~ Here, we put the for statement block in a closure, and then call this function, when the function call is complete, the variable I automatically destroyed, therefore, we can not access outside the block.

The closure features of JS are the most important feature ((*^__^*) you understand). In JS, in order to prevent naming conflicts, we should try to avoid using global variables and global functions. So, how to avoid it? Yes, as shown in the demo above, we can put all the content we want to define into a

[JavaScript]
    1. (function () {
    2. Content
    3. })();

JS scope and block level scope

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.