最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Object - 'Static Initialization' - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question asked Jun 30, 2010 at 19:21 JasonJason 1391 gold badge2 silver badges4 bronze badges 5
  • How are you defining classes in JavaScript? – Maz 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. – Byron Whitlock 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) { ... } – Jason Commented Jun 30, 2010 at 19:30
  • the static code runs when the first instance is created? – gblazex 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. – Jason Commented Jun 30, 2010 at 19:33
Add a comment  | 

4 Answers 4

Reset to default 7

Yes, there are some trick with ES6 classes.

class MyClass {

    static #unused = MyClass.#init();

    static #init() {
        // Your initialization logic
    }
}

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();
// 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;

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

class MyObject {
    static {
        // logic
    }
}
发布评论

评论列表(0)

  1. 暂无评论