Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variables in a Javascript class

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`​

DEMO

Or is there some other better way?

like image 963
Naftali Avatar asked May 31 '12 13:05

Naftali


People also ask

Can a class be static in JavaScript?

The static keyword defines a static method or property for a class, or a class static initialization block (see the link for more information about this usage). Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself.

Can I declare variable in class JavaScript?

To declare a variable within a class, it needs to be a property of the class or, as you did so, scoped within a method in the class. It's all about scoping and variables are not supported in the scope definition of a class.

Are static variables inherited in JavaScript?

method = ... Static properties and methods are inherited. For class B extends A the prototype of the class B itself points to A : B.

What is a static class JS?

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new operator to create a variable of the class type.


1 Answers

Yes, that is the correct approach to create private static variables.

However, I would treat the static_fn different. It seems you want it to be public.

  1. It should be on your "class"es prototype as it does not interact with private instance variables
  2. It even does not interact with instances at all. The usual approach is to put such a function/variable on the "class" itself, i.e. the constructor in JS. As the constructor is a Function object, it can be extended with properties as any other js object.
var ObjClass = (function closure(){

    var static_var = 0; //static private (scoped) variable
    function static_fn(){ return static_var; }; //static private (scoped) function

    function ObjClass() {
        var thisNumber = ++static_var; // private instance variable
        this.getThisNumber = function() { // public instance method
            return thisNumber; // "privileged" to access scoped instance variables
        };
    }
    ObjClass.getStaticNumber = static_fn; // make the static_fn public
    return ObjClass;
})();



var obj1 = new ObjClass;
var obj2 = new ObjClass;
console.log(ObjClass.getStaticNumber()); //output `2`
var obj3 = new ObjClass;
console.log(ObjClass.getStaticNumber()); //output `3`

console.log(obj1.getThisNumber()); //output `1`
console.log(obj2.getThisNumber()); //output `2`
console.log(obj3.getThisNumber()); //output `3`
like image 184
Bergi Avatar answered Sep 27 '22 20:09

Bergi