Advertisement
HJin_me

static counter

Oct 27th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(global){
  2.  
  3.     var counter = 0;    // 局部变量
  4.  
  5.     // 局部变量
  6.     var oneClass = function() {
  7.         this.init();
  8.     };
  9.     // 对象被创建时,调用该函数,计数器加一
  10.     oneClass.prototype.init = function() {
  11.         counter ++;
  12.     };
  13.     // 将局部变量返回,构造一个闭包
  14.     oneClass.prototype.getcount = function() {
  15.         return counter;
  16.     }
  17.  
  18.     // 将局部变量绑定到全局变量上
  19.     global.oneClass = oneClass;
  20.  
  21. }(window));
  22.  
  23.  
  24. var a = new oneClass();
  25. a.getcount();
  26. // return 1
  27. var b = new oneClass();
  28. b.getcount();
  29. // return 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement