current position:Home>JS function scope, closure, let, const

JS function scope, closure, let, const

2021-08-27 06:10:55 Upwind boy

What is a closure ?

simply , Closure is to include a function in a function , Internal functions have access to variables declared by external functions ; Closure involves function scope 、 Memory mechanism 、 Knowledge about function and chain

1、 Function scope

The so-called function scope is , Declare a function , Create a private scope in memory —— An enclosed space

Function fn(){
var a = 11;
}
fn();
 Copy code 

Beyond this scope, you will no longer access this variable .

2、 Memory recovery mechanism

The so-called memory recycling mechanism is that the system automatically recycles and cleans up the memory that is not needed , Make room for other programs to use .

 Insert picture description here

Scope chain

function fn(){
var a =234;
 return function(){
 console.log(a);
 }
 }
 Copy code 

The scope chain is in the scope of a function , Function which scope of the name to execute first , If there is no definition, keep looking up , Until you find the global scope

The function of closures

The function of closures is simply to protect and preserve .

  1. Protection :

Store variables in a private scope , Avoid conflicts because of the same name 2、 Preservation function : Use the tab to save data

let and const The role of 【 important 】

let and const Its function is as follows :

  • It is forbidden to repeat the statement

  • Supports block level scopes

  • Control modification

contrary , use var Declared variables : Can be repeated 、 There is no block-level scope 、 It can't be limited .

for Examples of cycles 【 Classic case 】

Code 1、 Let's first look at the following code :( use var Defining variables i)

<!DOCTYPE html>
<html lang="">
  <head>
    <meta />
    <meta />
    <meta />
    <title>Document</title>
  </head>
  <body>
    <input type="button" value="aa" />
    <input type="button" value="bb" />
    <input type="button" value="cc" />
    <input type="button" value="dd" />

    <script> var myBtn = document.getElementsByTagName("input"); for (var i = 0; i < myBtn.length; i++) { myBtn[i].onclick = function() { alert(i); }; } </script>
  </body>
</html>
 Copy code 

The operation effect in the above code is as follows :

You may be surprised , Why click any button , The pop-up content is 4 Well ? This is because , We use it var Variables defined i, Is declared at the global scope . In the whole code , There is only one variable from beginning to end . Before we hit the button , Variable i Has been cycled to 4 了 .

in other words , above for loop , Equivalent to the following code :

	var i = 0;
	myBtn[0].onclick = function() {
	alert(i);
	};
	i++;

	myBtn[1].onclick = function() {
	alert(i);
	};
	i++;

	myBtn[2].onclick = function() {
	alert(i);
	};
	i++;

	myBtn[3].onclick = function() {
	alert(i);
	};
	i++;  //  Come here ,i  The value of is already 4 了 . therefore , When we click the button ,i The value of has always been 4
 Copy code 

Code 2、 In the above code , If we change to let Defining variables i:

<!DOCTYPE html>
<html lang="">
  <head>
    <meta />
    <meta />
    <meta />
    <title>Document</title>
  </head>
  <body>
    <input type="button" value="aa" />
    <input type="button" value="bb" />
    <input type="button" value="cc" />
    <input type="button" value="dd" />

    <script> var myBtn = document.getElementsByTagName("input"); for (let i = 0; i < myBtn.length; i++) { myBtn[i].onclick = function() { alert(i); }; } </script>
  </body>
</html>
 Copy code 

The operation effect in the above code is as follows :

The above running results , That's what we expect . We use it let Defining variables i, During the cycle , Each time the loop is executed , A new... Will be born i. Loop body execution 4 Time , There will be four i.

Deconstruction and assignment of variables

ES6 Allow us , By means of arrays or objects , Assign values to a set of variables , This is called deconstruction .

Deconstruction assignment can greatly reduce the amount of our code in practical development , And make the program structure clearer .

copyright notice
author[Upwind boy],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210827061052216q.html

Random recommended