Is there a correct way to create private static javascript variables (and functions) that do not change no matter how many times you create new Obj?
This is what I tried and it seems to work:
var ObjClass = (function(){
var static_var = 0; //private static variable
var static_fn = function(){ return static_var; }; //private static function
return function(){
++static_var;
var thisNumber = static_var;
this.getThisNumber = function(){
return thisNumber;
}
this.getStaticNumber = static_fn; //making static fn public
}
})();
var obj1 = new ObjClass;
var obj2 = new ObjClass;
var obj3 = new ObjClass;
console.log(obj1.getThisNumber()); //output `1`
console.log(obj1.getStaticNumber()); //output `3`
console.log(obj2.getThisNumber()); //output `2`
console.log(obj2.getStaticNumber()); //output `3`
console.log(obj3.getThisNumber()); //output `3`
console.log(obj3.getStaticNumber()); //output `3`
Or is there some other better way?
()after each call tonew ObjClass.