7

I'm converting some Java code to Javascript, and the Java object has a Static Initialization block that populates two arrays in the object. My understanding is that this initializer runs only once no matter how many objects are created. Can I do such a thing in Javascript?

Java code:

    public final class MyObject {

        private MyObject() { }

        // ...

    static {
             // Run once static init code here
    }

}

Can this run-once style initialization be done in Javascript?

Thanks

5
  • How are you defining classes in JavaScript? Commented Jun 30, 2010 at 19:25
  • 1
    Converting java code to JavaScript? huh? Their use cases are totally different... remember JavaScript is NOT a subset superset, or even an intersect with java. they just share the first 4 letters. Commented Jun 30, 2010 at 19:27
  • 1
    Ok, I have some code for something in Java that I would like to also do in Javascript. In Javascript my class is defined like this: function MyClass() { this.name = ""; } MyClass.prototype.doStuff = new function(a,b) { ... } Commented Jun 30, 2010 at 19:30
  • the static code runs when the first instance is created? Commented Jun 30, 2010 at 19:31
  • 1
    @galambalazs yes, that's what im after. Just a function that runs only when the first instance is created, and ignored on all following instantiations. I know I could do some hacky solutions using global flags, but there must be a neater way. Commented Jun 30, 2010 at 19:33

4 Answers 4

7

Yes, there are some trick with ES6 classes.

class MyClass {

    static #unused = MyClass.#init();

    static #init() {
        // Your initialization logic
    }
}
Sign up to request clarification or add additional context in comments.

Comments

6

Not really.

The whole concept of "static" members doesn't really apply to javascript. You can achieve them but only in a "public" way.

This sort of does what you're asking for, but it's really just a bunch of kludgy syntax over "run this function once as triggered by a constructor".

function MyObject()
{
  if ( 'undefined' == typeof MyObject.__initialized )
  {
    // static stuff
    alert( 'hi' );

    MyObject.__initialized = true;
  }

  // Proceed with constructing instance of MyObject
}

new MyObject();
new MyObject();

1 Comment

@Peter, Does this solution interact well with workers/threads?
4
// Object Contructor
function MyObject(name) {

  if (!this.done) {
    this.done = true;
    // init stuff
    // ...
  }

  this.name = name;
  return this; // optional
}

// available in all instances
MyObject.prototype.done = false;

Comments

1

Yes, it's possible now with class static initialization blocks ES2022 feature:

class MyObject {
    static {
        // logic
    }
}

1 Comment

Thank you, does it support inheritance too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.