• 方式一 ```javascript function fn(){ var num = 0;

      function add(){ num ++; console.log(num); };

      function sub(){ num —; console.log(num); }

      return [add, sub]; }

    var test = fn() test0; test1;

    1. - 方式二
    2. ```javascript
    3. function fn(){
    4. var num = 0;
    5. var compute = {
    6. add: function(){
    7. num ++;
    8. console.log(num);
    9. },
    10. sub: function(){
    11. num --;
    12. console.log(num);
    13. }
    14. }
    15. return compute;
    16. }
    17. var test = fn()
    18. test.add();
    19. test.sub();
    • 方式三 ```javascript function fn(){ var num = 0;

      this.add = function(){ num ++; console.log(num); };

      this.sub = function(){ num —; console.log(num); } }

    var test = new fn();

    test.add(); test.sub();

    因为new时fn函数会默认return this 如果手动在fn函数最后return 原始值,不会影响,return 引用值,则会影响; ```