方式一 ```javascript function fn(){ var num = 0;
function add(){ num ++; console.log(num); };
function sub(){ num —; console.log(num); }
return [add, sub]; }
- 方式二```javascriptfunction fn(){var num = 0;var compute = {add: function(){num ++;console.log(num);},sub: function(){num --;console.log(num);}}return compute;}var test = fn()test.add();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 引用值,则会影响; ```
