当前位置:网站首页>Closure problem

Closure problem

2022-06-27 07:43:00 Beauty of algorithm and programming

  1. Problem description

A closure is a vector that can access other scopes , Such a function is called a closure .

2. Algorithm description

Define a function outer, When outer When the function is first executed in , Declare a local variable a=100, And declare the first function inner Function and put inner The function declaration part returns , When executed again , There are no variables in its scope a, Query up the scope chain , And every time I add 10.

3. Experimental discussion and results

Find out the internal logic , And write the process , Bring in value , Output results .

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
</html>
<script type="text/javascript">
function outer (){
var a=100;
function inner(){
a+=10
console.log(a);
}
return inner;
}
var result =outer();
result();
result();
var result1=outer();
   result1();
</script>

Four . Conclusion

The closure problem is mainly to understand the primary and secondary relationships in this logic , In the large framework, we can carry out small operations at all levels . Different declaration parts determine different results , When a new calculation is performed again .

原网站

版权声明
本文为[Beauty of algorithm and programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270736342206.html