What is the use of javascript block level scope?

青灯夜游
Release: 2022-01-18 12:03:42
Original
2719 people have browsed it

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:

Copy after login

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(){

  //这里是块级作用域

})();
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!