Home  >  Article  >  Web Front-end  >  What is the use of javascript block level scope?

What is the use of javascript block level scope?

青灯夜游
青灯夜游Original
2022-01-18 12:03:422913browse

The role of block-level scope: resolve naming conflicts caused by too many global variables and functions. JavaScript will turn a blind eye to repeated declarations, causing errors in execution results. Block-level scope can solve this problem, allowing each developer to use his own variables without worrying about messing up the global scope.

What is the use of javascript block level scope?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

What is block-level scope?

Any set of statements in a pair of curly braces ({ and }) belongs to a block. All variables defined in it are invisible outside the code block. We call it block-level action. Domains, such as for, while, if

and JavaScript does not have block-level scope (before the es6 standard came out, JavaScript did not have block-level scope), that is, define a variable in the JavaScript code block , the variable can still be used outside the code block, for example:

<script type="text/javascript">  
   var i=1;  
     if(i==1){  
        var b=2;  
     }  
   alert(b);//输出2  

   function outputNumbers(count){
       for(vari=0;i<count;i++){
           alert(i);
       }
           alert(i); //计数
    } 
</script>

In languages ​​such as Java and C, the variable i will only be defined in the statement block of the for loop. Once the loop ends, the variable i will will be destroyed. But in JavaScript, the variable i is defined in the active object of outputNumbers(), so it can be accessed anywhere inside the function from the time it is defined.

Why should we implement block-level scope in JavaScript and what is its use?

Block-level scope can solve naming conflicts caused by too many global variables and functions, because JavaScript will turn a blind eye to repeated declarations (however, it will perform variable initialization in subsequent declarations), resulting in running results Something goes wrong, and you'll probably have to go to great lengths to catch it.

In a large application written by many developers, the use of private scopes allows each developer to use his or her own variables without worrying about messing up the global scope.

How JavaScript implements block-level scope

Although there is no block-level scope in js, closures/anonymous functions can be used to imitate block-level scope.

The syntax for anonymous functions in block-level scope (often called private scope) is as follows:

(function(){

  //这里是块级作用域

})();

The above code defines and immediately calls an anonymous function. Enclosing a function declaration within a pair of parentheses indicates that it is actually a function expression. Another pair of parentheses immediately following it will immediately call this function.

An example of using anonymous functions to implement block-level scope in JavaScript

function outputNumbers(count){
   (function(){
      for(var i=0;i<count;i++){
          alert(i);
         }
        })();
    alert(i); //导致错误!
   }

In the example, any variables defined in the anonymous function will be destroyed at the end of execution. Therefore, the variable i can only be used within the loop and is destroyed after use. The variable count can be accessed in the private scope because this anonymous function is a closure, which can access all variables in the containing scope.

When can you use private scope

No matter where you are, as long as you need some variables temporarily, you can use private scope

[Related recommendations:javascript learning tutorial

The above is the detailed content of What is the use of javascript block level scope?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn