title: 'Cannot find function in object' error after moving class definition to separate .gs file
body: <p>Imagine you have one Code.gs file for all your Apps Script code. It has some sort of JavaScript "class" Apple, global and local variables of the class and function that is using them:</p>
  
  <pre><code>function Apple (type) {
      this.type = type;
      this.color = "red";
  }
  
  Apple.prototype.getInfo = function() 
  {
      return this.color + ' ' + this.type + ' apple';
  };
  
  var globalAppleItem = new Apple('global');
  
  function onOpen() {
    var ss = SpreadsheetApp.getActive();
    var items = [
      {name: 'AppleItem test', functionName: 'menuItem1'},
    ];
    ss.addMenu('Custom Menu', items);
  }
  
  function menuItem1() {
    var localAppleItem = new Apple('local');
    Logger.log('localAppleItem.getInfo()=' + localAppleItem.getInfo());
    Logger.log('globalAppleItem.color=' + globalAppleItem.color);
    Logger.log('globalAppleItem.getInfo()=' + globalAppleItem.getInfo());
  }
  </code></pre>
  
  <p>This script executes fine:</p>
  
  <blockquote>
  <pre><code>Script execution errors:
  No 
  
  Logger output:
  [14-03-10 17:18:29:309 GST] localAppleItem.getInfo()=red local apple
  [14-03-10 17:18:29:309 GST] globalAppleItem.color=red
  [14-03-10 17:18:29:309 GST] globalAppleItem.getInfo()=red global apple
  </code></pre>
  </blockquote>
  
  <p>After some time you decide that moving the Apple class definition to a separate file is a good idea:</p>
  
  <p>Apple.gs:</p>
  
  <pre><code>function Apple (type) {
      this.type = type;
      this.color = "red";
  }
  
  Apple.prototype.getInfo = function() 
  {
      return this.color + ' ' + this.type + ' apple';
  };
  </code></pre>
  
  <p>Code.gs:</p>
  
  <pre><code>var globalAppleItem = new Apple('global');
  
  function onOpen() {
    var ss = SpreadsheetApp.getActive();
    var items = [
      {name: 'AppleItem test', functionName: 'menuItem1'},
    ];
    ss.addMenu('Custom Menu', items);
  }
  
  function menuItem1() {
    var localAppleItem = new Apple('local');
    Logger.log('localAppleItem.getInfo()=' + localAppleItem.getInfo());
    Logger.log('globalAppleItem.color=' + globalAppleItem.color);
    Logger.log('globalAppleItem.getInfo()=' + globalAppleItem.getInfo());
  }
  </code></pre>
  
  <p>But behavior becomes strange:</p>
  
  <blockquote>
    <p>Script execution errors: TypeError: Cannot find function getInfo in
    object [object Object]</p>
    
    <p>Logger output: [14-03-10 17:16:08:830 GST]
    localAppleItem.getInfo()=red local apple [14-03-10 17:16:08:830 GST]
    globalAppleItem.color=red</p>
  </blockquote>
  
  <p>By some means Apps Script "doesn't see" getInfo method definition. And at the same time, property color exists and initialized.
  I spent much time experimenting and searching the Web. Finally I found a solution which I'll provide as an answer to this post. But precise explanation of this behavior is what I haven't got yet and it would be great if someone could provide one.</p>