title: JavaScript - check if in global context
body: <p>When a function is attached to an object and called:</p>
  
  <pre><code>function f() { return this.x; }
  var o = {x: 20};
  o.func = f;
  o.func(); //evaluates to 20
  </code></pre>
  
  <p><code>this</code> refers to the object that the function was called as a method of. It's equivalent to doing <code>f.call(o)</code>.</p>
  
  <p>When the function is called not as part of an object, <code>this</code> refers to the global object. How do I check if a function is being called from a non-object context? Is there any standard keyword to access the global object? Is the only way to do it something like this?</p>
  
  <pre><code>globalobj = this;
  function f() { if (this == globalobj) doSomething(); }
  </code></pre>
  
  <p>Note: I have no particular use case in mind here - I actually am asking about this exact mechanism.</p>