js闭包

<!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>
原文地址:https://www.cnblogs.com/angdh/p/13973964.html