分享

private static member in javascript

 moonboat 2008-10-01
原文链接
http://www.litotes./js_info/private_static.html
期待简译。

有一些代码,整得有些过了,但还是很值得参考。在jct的代码中看到了类似的用法。
在js中隐藏变量还是有必要的,否则和其他的js库冲突,就会很麻烦,很难找到bug。

贴几段代码,明白人都看得懂。
Javascript代码 复制代码
  1. var MyObject = (function(){   
  2.     /*private static (class) member*/  
  3.     var counter = 0;   
  4.     /*private static method.*/  
  5.     function incrementCounter(){   
  6.         return counter++;   
  7.     };   
  8.     /*class constructor.*/  
  9.     function constructorFn(id){   
  10.         this.id = id;   
  11.         var self = this;   
  12.         /*call private static (class)  
  13.         method and assign the returned  
  14.         index to a private instance member.*/  
  15.         var index = incrementCounter();   
  16.         /*privileged instance method.*/  
  17.         this.getIndex = function(){   
  18.             return index;   
  19.         };   
  20.     };   
  21.     /*privileged static (class) method  
  22.     (a property of the constructor)*/  
  23.     constructorFn.getNoOfInsts = function(){   
  24.         return counter;   
  25.     };   
  26.     /*public instance method privaliged at the  
  27.     class level*/  
  28.     constructorFn.prototype.pubInstMethod = function(){   
  29.         ...   
  30.     };   
  31.     /*return the constructor.*/  
  32.     return constructorFn;   
  33. })(); //simultaneously define and call (one-off)!   
  34.   
  35. /*public static  member*/  
  36. MyObject.pubStatic = "anything"  
  37.   
  38. /*public instance member*/  
  39. MyObject.prototype.pubInstVar = 8;  


再来一段
Javascript代码 复制代码
  1. var global = this;   
  2. (function(){   
  3.     var classGroupMember = 3;   
  4.     function privateToClassGroup(){   
  5.         /*this method could be a utilitiy  
  6.         for the classes in the group or used  
  7.         as an internal object constructor.*/  
  8.         ...   
  9.     };   
  10.     global.MyObject1 = function(){   
  11.         var privteStaticMember = 4;   
  12.         function privateStaticMethod(){   
  13.             ...   
  14.         };   
  15.         function constructorFn(id){   
  16.             ...   
  17.         };   
  18.         /*return the constructor.*/  
  19.         return constructorFn;   
  20.     }();  //simultaneously define and call!   
  21.     global.MyObject2 = function(){   
  22.         function constructorFn(id){   
  23.             ...   
  24.         };   
  25.         /*return the constructor.*/  
  26.         return constructorFn;   
  27.     }();  //simultaneously define and call!   
  28.     global.MyObject3 = function(){   
  29.         function constructorFn(id){   
  30.             ...   
  31.         };   
  32.         /*return the constructor.*/  
  33.         return constructorFn;   
  34.     }();  //simultaneously define and call!   
  35. })();  //simultaneously define and call!  


再来一段:
Javascript代码 复制代码
  1. /*constructor function */  
  2. function MyObject(){   
  3.     ...   
  4. }   
  5.   
  6. MyObject.prototype = (function(){   
  7.     /*private static (class) member*/  
  8.     var privateStaticProp = "anything";   
  9.     /*private static method .*/  
  10.     function privateStatic Method = function(){   
  11.         ...   
  12.     }   
  13.     return ({   
  14.         /*These functions objects are shared by  
  15.         all instances that uses this prototype  
  16.         and they have access to the private static  
  17.         members within the closure that returns  
  18.         this object*/  
  19.         publicInstanceMethod:function(){   
  20.             ...   
  21.         },   
  22.         setSomething:function(s){   
  23.             privateStaticProp = s;   
  24.         }   
  25.     });   
  26. })();   
  27.   
  28. /*public instance member*/  
  29. MyObject.prototype.pubInstVar = 8;  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多