title: How to use a class instance as a static property?
body: <p>I want to create a static property for a class named OuterClass. I want the value of this property to be an instance of another class, which is named <code>InnerClass</code>.</p>
  <p>Here is the inner class. It has a property and a function.</p>
  <pre><code>// InnerClass.gs
  
  function InnerClass() {
    this.myProperty = 42;
  }
  
  InnerClass.prototype.myFunction = function() {
    return 43;
  };
  </code></pre>
  <p>And here is the outer class, which has only one static property.</p>
  <pre><code>// OuterClass.gs
  
  function OuterClass() {
  }
  OuterClass.innerClass = new InnerClass();
  </code></pre>
  <p>However, when I try to call methods of the inner class, I get:</p>
  <blockquote>
  <p>TypeError: Cannot find function myFunction in object [object Object].</p>
  </blockquote>
  <pre><code>// myScript.gs
  
  function myScript() {
  
    console.log(OuterClass.innerClass.myProperty);   // 42.0
    console.log(OuterClass.innerClass.myFunction()); // TypeError: Cannot find function myFunction in object [object Object].
  
    var anotherInnerClassInstance = new InnerClass();
    console.log(anotherInnerClassInstance.myFunction()); // 43.0
  }
  </code></pre>
  <p>Since calling the instance method on <code>anotherInnerClassInstance</code> works, I believe that I am having trouble with the static property <code>OuterClass.innerClass</code> because</p>
  <ol>
  <li>The constructor of <code>InnerClass</code> is hoisted, but <code>InnerClass.prototype.myFunction</code> is not.</li>
  <li>As <code>OuterClass.innerClass</code> is instantiated, it is instantiated with an incomplete instance, because <code>InnerClass.prototype.myFunction</code> was not hoisted, and is not yet attached to the instance created.</li>
  </ol>
  <p>Is there a way to use a class instance as a static variable? Note that I have to use prototype-based classes because I'm really using Google Apps Script, which is based on an obsolete version of JavaScript.</p>
  <hr />
  <p>For those who have been unable to replicate this problem, here is a link to the Google Sheet producing this error:
  <a href="https://docs.google.com/spreadsheets/d/1Gxylcrbg9rWHGmc68CgHFmZqJ20E5-pLgA6fmHkxhAA/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/1Gxylcrbg9rWHGmc68CgHFmZqJ20E5-pLgA6fmHkxhAA/edit?usp=sharing</a></p>
  <p>Also, here's a direct link to the script project: <a href="https://script.google.com/d/1V0FYrgiB3a4rTtvd9StyDtWAZ13AqlPl4rpgauCWSKk46UbcdIj9nqJC/edit?usp=sharing" rel="nofollow noreferrer">https://script.google.com/d/1V0FYrgiB3a4rTtvd9StyDtWAZ13AqlPl4rpgauCWSKk46UbcdIj9nqJC/edit?usp=sharing</a></p>