首页 > Web开发 > 详细

js闭包

时间:2020-11-14 17:37:22      阅读:31      评论:0      收藏:0      [点我收藏+]

 

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <script type="text/javascript">

      function a(){
        var start = 0;
        function b(){
          return start++;
        }
        return b;
      }
      var inc = a();
      console.log(inc());
      console.log(inc());
      console.log(inc());
      
  </script>
</body>
</html>

 

 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <script type="text/javascript">

      function Person(name){

          var age;

          function setAge(n){
              age = n;
          }
          function getAge(){
            return age;
          }
          return {
               name:name,
               setAge:setAge,
               getAge:getAge
          }

      }


        var p1 = Person(‘ssj‘);
        p1.setAge(22);
        console.log(p1.getAge());


  </script>
</body>
</html>

 

js闭包

原文:https://www.cnblogs.com/angdh/p/13973964.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!