原文鏈接
http://www.litotes.demon.co.uk/js_info/private_static.html 期待簡譯。
有一些代碼,整得有些過了,但還是很值得參考。在jct的代碼中看到了類似的用法。
在js中隱藏變量還是有必要的,否則和其他的js庫沖突,就會很麻煩,很難找到bug。
貼幾段代碼,明白人都看得懂。
- var MyObject = (function(){
-
- var counter = 0;
-
- function incrementCounter(){
- return counter++;
- };
-
- function constructorFn(id){
- this.id = id;
- var self = this;
-
-
-
- var index = incrementCounter();
-
- this.getIndex = function(){
- return index;
- };
- };
-
-
- constructorFn.getNoOfInsts = function(){
- return counter;
- };
-
-
- constructorFn.prototype.pubInstMethod = function(){
- ...
- };
-
- return constructorFn;
- })();
-
-
- MyObject.pubStatic = "anything"
-
-
- MyObject.prototype.pubInstVar = 8;
var MyObject = (function(){/*private static (class) member*/var counter = 0;/*private static method.*/function incrementCounter(){return counter++;};/*class constructor.*/function constructorFn(id){this.id = id;var self = this;/*call private static (class)method and assign the returnedindex to a private instance member.*/var index = incrementCounter();/*privileged instance method.*/this.getIndex = function(){return index;};};/*privileged static (class) method(a property of the constructor)*/constructorFn.getNoOfInsts = function(){return counter;};/*public instance method privaliged at theclass level*/constructorFn.prototype.pubInstMethod = function(){...};/*return the constructor.*/return constructorFn;})(); //simultaneously define and call (one-off)!/*public static member*/MyObject.pubStatic = "anything"/*public instance member*/MyObject.prototype.pubInstVar = 8;
再來一段
- var global = this;
- (function(){
- var classGroupMember = 3;
- function privateToClassGroup(){
-
-
-
- ...
- };
- global.MyObject1 = function(){
- var privteStaticMember = 4;
- function privateStaticMethod(){
- ...
- };
- function constructorFn(id){
- ...
- };
-
- return constructorFn;
- }();
- global.MyObject2 = function(){
- function constructorFn(id){
- ...
- };
-
- return constructorFn;
- }();
- global.MyObject3 = function(){
- function constructorFn(id){
- ...
- };
-
- return constructorFn;
- }();
- })();
var global = this;(function(){var classGroupMember = 3;function privateToClassGroup(){/*this method could be a utilitiyfor the classes in the group or usedas an internal object constructor.*/...};global.MyObject1 = function(){var privteStaticMember = 4;function privateStaticMethod(){...};function constructorFn(id){...};/*return the constructor.*/return constructorFn;}(); //simultaneously define and call!global.MyObject2 = function(){function constructorFn(id){...};/*return the constructor.*/return constructorFn;}(); //simultaneously define and call!global.MyObject3 = function(){function constructorFn(id){...};/*return the constructor.*/return constructorFn;}(); //simultaneously define and call!})(); //simultaneously define and call!
再來一段:
-
- function MyObject(){
- ...
- }
-
- MyObject.prototype = (function(){
-
- var privateStaticProp = "anything";
-
- function privateStatic Method = function(){
- ...
- }
- return ({
-
-
-
-
-
- publicInstanceMethod:function(){
- ...
- },
- setSomething:function(s){
- privateStaticProp = s;
- }
- });
- })();
-
-
- MyObject.prototype.pubInstVar = 8;