title: What is the context of an anonymous function?
body: <p>I have code like this:</p>
  
  <pre><code>function demo() {
      this.val=5;
      function() {
          this.val=7;
      }();
  }
  </code></pre>
  
  <p>Now when I give execute this code in the firefox or chrome console it gives a syntax error. I don't understand why this is an error because I have read that javascript functions are objects so when I call the anonymous function, inside it <code>this</code> points to function demo and should change the <code>val</code> to <code>7</code>, so if I do</p>
  
  <pre><code>var x=new demo();
  x.val;   //should give 7
  </code></pre>
  
  <p>but when I do this </p>
  
  <pre><code>function demo() {
      this.val=5;
      var f=function() {
              this.val=7;
      }();
  }
  window.val;    // gives 7
  </code></pre>
  
  <p>I don't understand if functions are objects then why the <code>this</code> in the anonymous function points to <code>window</code> and not <code>demo</code>. 
  Please explain this.</p>