title: Why am I getting "Cannot read property 'text' of undefined" in this example?
body: <p>I'm in the progress of making a game, which you can by clicking <a href="http://playclassicsnake.com/play" rel="nofollow">here</a>. My first question is why I'm getting </p>
  
  <blockquote>
    <p>Cannot read property 'text' of undefined</p>
  </blockquote>
  
  <p>when the snake (green blocks) hits the food (red circle).</p>
  
  <p>The line that is invoking the error is the <code>this.scoreElem.text(newScore);</code> from</p>
  
  <pre><code>function StatHandler ( totalScoreSelector, fruitEatenSelector )
  {
      this.scoreElem = $(totalScoreSelector);
      this.fruitEeatenElem = $(fruitEatenSelector);
  
      this.incrementScore = function ( incAmount )
      {
          $.ajax({
              type: "POST",
              url: "Play/IncrementScore?amount=" + incAmount,
              success: function (newScore)
              {
                  this.scoreElem.text(newScore);
              },
              error: function ( ) 
              {
                  alert("error occured!");
              }
          });
      }
  }
  </code></pre>
  
  <p>The instance of a <code>StatHandler</code> object is created by </p>
  
  <pre><code>var H = new StatHandler("#total-score", "#fruit-eaten"); 
  </code></pre>
  
  <p>inside a <code>$(document).ready</code>, where I've verified that <code>#total-score</code> and <code>#fruit-eaten</code> are valid selectors since they are <code>id</code>s of elements I've verified to be in the DOM.</p>
  
  <p>Full code can be seen <a href="https://github.com/jamkin/Snake/blob/master/SnakeGame/Scripts/game.js" rel="nofollow">here</a>. </p>
  
  <p>Also, as you may have noticed, I'm trying to update the score on the server side and send the updated score back to the client. But, this doesn't prevent the client from cheating by running </p>
  
  <pre><code>SG.stats.incrementScore(1000000000);
  </code></pre>
  
  <p>in the JS console. How do I prevent cheating? </p>