Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Constructor in Javascript ES6

In ES6, I can create static methods like below. But I need to define a static constructor but no success. I need something that runs only once when the class is loaded. I Is there any way to implement something like this ?

class Commander{

    static onData(){
         console.log("blabla");
    }
}
like image 933
Barış Velioğlu Avatar asked Feb 02 '16 14:02

Barış Velioğlu


People also ask

What is static method in ES6?

Static methods are often used to create utility functions for an application.” In other words, static methods have no access to data stored in specific objects. Note that for static methods, the this keyword references the class. You can call a static method from another static method within the same class with this.

What is constructor in JavaScript ES6?

A constructor is a function that is called each time an object is created (also referred to as instantiated). The User constructor creates the properties of the object (this.name, this. age, this. email) and assigns them the value of the parameters passed to it (name, age, email).

What is a static function in JS?

A static method in JavaScript is a method that has a static keyword prepended to itself. Such methods cannot be accessed through instantiated objects but could be accessed through the class name. This is because static methods belong to the class directly. Inheritance even applies to static methods.

Can I use static in JavaScript?

With JavaScript Classes, we can add methods and properties that can be accessed per instance of the Class. This is standard behavior when you make multiple instances of a Class. If we create a method that does not access an instance property, we can use the static keyword.


2 Answers

It does seem neater to have class-setup code inside the class body so the "class expression" is self-contained. ES6 accepts the syntax static constructor() {/* do stuff */} in a class body but never runs it. Perhaps it is for future language expansion? Anyway, here is one way to achieve the desired result. The trick is to initialize a static property with an immediately-executed function expression that does your class setup:

class MyClass {
  static _staticConstructorDummyResult = (function() {
    console.log('static constructor called') // once!
  })()
  constructor () {
    console.log('instance constructor called')
  }
}
let obj = new MyClass(),
    obj2 = new MyClass()

Inside the "static constructor" you can add properties to the class object with MyClass.prop = value, or if you're keen to refer to MyClass as this, change the function expression to an arrow function expression.

You can make _staticConstructorDummyResult non-enumerable using Object.defineProperty(), or if you don't mind requiring Chrome (it won't work in Firefox currently), you can add a # at the front of the name to make it a private property.

like image 146
Hugh Allen Avatar answered Oct 13 '22 20:10

Hugh Allen


I need something that runs only once when the class is loaded.

You shouldn't be using classes if you just use them as a bag of methods. Use an object instead. However, it's still possible to run such code. Just put it before or after the class definition.

console.log('before class is created')

class Foo {}

console.log('after class was created');
like image 24
Felix Kling Avatar answered Oct 13 '22 20:10

Felix Kling