免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
private static member in javascript
原文鏈接
http://www.litotes.demon.co.uk/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;  
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
JavaScript面向對象的支持
20個JS優(yōu)化代碼技巧
JavaScript中的JSON
Lua Classes and Packages in Corona
PHP程序員如何理解依賴注入容器(dependency injection container)
javascript的一個實例
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服