title: javascript constructor of anonymous object
body: <p>After moving from PHP to NodeJS (JavaScript) I am having some trouble with JavaScript's Anonymous Objects - I can't figure out how to create a constructor to an anonymous object like you would have in PHP with the <code>__construct()</code> function or Python's <code>__init__(self)</code> function. Maybe like Java <a href="https://stackoverflow.com/questions/362424/accessing-constructor-of-an-anonymous-class">[Link]</a> JavaScript can't have anonymous constructors...</p>
  
  <h2>Solution:</h2>
  
  <p>After reading the comments and other answers, I came to find out that by using an anonymous function that returned an object you can gain this functionality:</p>
  
  <pre><code>var a = new function() {
    var constTxt = [not constructed]';
  
    /* emulating a constructor */
    if (true) {
       constTxt = '[constructed]';
    }
  
    return {
      functionABC : function() {
        return 'abc ' +  constTxt;
      },
      functionDCF : function() {
        return 'other function';
      }
    };
  }; 
  </code></pre>