前端学习(1835):前端面试题之闭包1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
               /*
 company: shangguigu
 author: yanzhiyong
 content: 闭包
*/


/*
* 理解:什么是闭包?
*   1. 密闭的容器,类似于set,map容器,存储数据的
*   2. 闭包是一个对象,存放数据的格式: key: value
* 形成的条件:
*   1. 函数嵌套
*   2. 内部函数引用外部函数的局部变量
* 闭包的优点:
*   延长外部函数局部变量的生命周期
* 闭包的缺点:
*   容易造成内存泄漏
* 注意点:
*   1. 合理的使用闭包
*   2. 用完闭包要及时清除(销毁)
* */

 function fun() {
  var count = 1;
  function fun2() {
    console.log(count);
  }

   fun2();
 }

fun(); 
    </script>
</body>
</html>

运行结果

猜你喜欢

转载自blog.csdn.net/weixin_43392489/article/details/107502175