JS has no block-level scope but has function scope

Source: Internet
Author: User

Any set of statements in a pair of curly braces belongs to a block, and all variables defined in it are not visible outside the code block, which we call a block-level scope.
Scope is always a priority in any programming language because it controls the visibility and life cycle of variables and parameters. First we 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 that 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:

        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

(function//}) ();

, is this the equivalent of adding a function scope to their outer layers? Programs outside of this scope cannot access them.

JS has no block-level scope but has function 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.