Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variables in JavaScript

How can I create static variables in Javascript?

like image 707
Rajat Avatar asked Oct 08 '09 04:10

Rajat


People also ask

Can we use static variable in JavaScript?

How can I create static variables in Javascript? we can define label or other html tag with "dispaly:none" style attribute and set variable value for this value and operation on this value. Let's not take hard. The simplest solution I found: don't define a static variable in the class at all.

What is an example of a static variable?

The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. The static variable gets memory only once in the class area at the time of class loading.

Where are static variables stored in JavaScript?

ANSWER. The initial values of global and static variables are stored in ROM in a segment called ? C_INITSEG. They are then copied to the relevant RAM locations after the code in STARTUP.

Which is the static variable?

In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program.


1 Answers

If you come from a class-based, statically typed object-oriented language (like Java, C++ or C#) I assume that you are trying to create a variable or method associated to a "type" but not to an instance.

An example using a "classical" approach, with constructor functions maybe could help you to catch the concepts of basic OO JavaScript:

function MyClass () { // constructor function   var privateVariable = "foo";  // Private variable     this.publicVariable = "bar";  // Public variable     this.privilegedMethod = function () {  // Public Method     alert(privateVariable);   }; }  // Instance method will be available to all instances but only load once in memory  MyClass.prototype.publicMethod = function () {       alert(this.publicVariable); };  // Static variable shared by all instances MyClass.staticProperty = "baz";  var myInstance = new MyClass(); 

staticProperty is defined in the MyClass object (which is a function) and has nothing to do with its created instances, JavaScript treats functions as first-class objects, so being an object, you can assign properties to a function.

UPDATE: ES6 introduced the ability to declare classes through the class keyword. It is syntax sugar over the existing prototype-based inheritance.

The static keyword allows you to easily define static properties or methods in a class.

Let's see the above example implemented with ES6 classes:

class MyClass {    // class constructor, equivalent to    // the function body of a constructor    constructor() {      const privateVariable = 'private value'; // Private variable at the constructor scope      this.publicVariable = 'public value'; // Public property        this.privilegedMethod = function() {        // Public Method with access to the constructor scope variables        console.log(privateVariable);      };    }      // Prototype methods:    publicMethod() {      console.log(this.publicVariable);    }      // Static properties shared by all instances    static staticProperty = 'static value';      static staticMethod() {      console.log(this.staticProperty);    }  }    // We can add properties to the class prototype  MyClass.prototype.additionalMethod = function() {    console.log(this.publicVariable);  };    var myInstance = new MyClass();  myInstance.publicMethod();       // "public value"  myInstance.additionalMethod(); // "public value"  myInstance.privilegedMethod(); // "private value"  MyClass.staticMethod();             // "static value"
like image 183
Christian C. Salvadó Avatar answered Sep 30 '22 00:09

Christian C. Salvadó