zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    动态表格
    Palindrome Number
    String to Integer (atoi) ???
    Reverse Integer
    Two Sum
    Path Sum
    Minimum Depth of Binary Tree
    Plus One
    Maximum Depth of Binary Tree
    Symmetric Tree
  • 原文地址:https://www.cnblogs.com/angdh/p/13973964.html
Copyright © 2011-2022 走看看