title: Objects in JavaScript defined and undefined at the same time (in a FireFox extension)
body: <p>I am chasing down a <a href="https://www.mozdev.org/bugs/show_bug.cgi?id=22480" rel="nofollow noreferrer">bug</a> in a FireFox extension. I've finally managed to see it for myself (I've only had reports before) and I can't understand how what I saw is possible.</p>
  
  <p>One error message from my extension in the Error Console is "gBrowser is not defined". This by itself would be surprising enough, since the overlay is over browser.xul and navigator.xul, and I expect <code>gBrowser</code> to be available from both. Even worse is the actual place where it happens: line 101 of <a href="http://gist.github.com/311567" rel="nofollow noreferrer">nextplease.js</a>. That is, inside the function <code>isTopLevelDocument</code>, which is only called from <code>onContentLoaded</code>, which is only called from <code>onLoad</code> here:</p>
  
  <pre><code>gBrowser.addEventListener(this.loadType, function (event) {
      nextplease.loadListener.onContentLoaded(event);
  },
  true);
  </code></pre>
  
  <p>So <code>gBrowser</code> is defined in <code>onLoad</code>, but somehow undefined in <code>isTopLevelDocument</code>.</p>
  
  <p>When I tried to actually use the extension, I got another error: "nextplease is not defined". The interesting thing is that it happened on lines 853 and 857. That is, <em>inside</em> the functions </p>
  
  <pre><code>nextplease.getNextLink = function () {
      nextplease.getLink(window.content, nextplease.NextPhrasesMap, nextplease.NextImagesMap, nextplease.isNextRegExp, nextplease.NEXT_SEARCH_TYPE);
  }
  
  nextplease.getPrevLink = function () {
      nextplease.getLink(window.content, nextplease.PrevPhrasesMap, nextplease.PrevImagesMap, nextplease.isPrevRegExp, nextplease.PREV_SEARCH_TYPE);
  }
  </code></pre>
  
  <p>So <code>nextplease</code> is somehow defined enough to call these functions, but isn't defined inside them.</p>
  
  <p>Finally, executing <code>typeof(nextplease)</code> in Execute JS returns "object". Same for <code>gBrowser</code>.</p>
  
  <p>How can this happen? Any ideas?</p>