title: Uncaught TypeError: Cannot read property '2' of undefined
body: <p>The following is code for a TicTacToe program. I'm trying to write OO. I'm getting a Uncaught TypeError: Cannot read property '2' of undefined in the checkIfPositionIsTaken function when I click a box on my grid.</p>
  
  <p>The following is the constructor for the TicTacToe Object:</p>
  
  <pre><code>var TicTacToe = function () {
  
  this.player1Name = "Player 1";
  this.player2Name = "Player 2";
  var turn = "";
  var grid = [[0,0,0],[0,0,0],[0,0,0]];
  var hasWinner = 0;
  var moveCount = 0;
  }
  </code></pre>
  
  <p>The following is the checkIfPositionIsTaken function. I believe the error is on this.grid but it is defined when the object is created.</p>
  
  <pre><code>// Check If Position Is Taken
  TicTacToe.prototype.checkIfPositionIsTaken = function(row, col){
     if(this.grid[row][col] !== 0){
        alert("This position is taken. Please try other position.");
        return true;
  }
  else{
      return false;
  }
  };
  </code></pre>
  
  <p>The following is where I create the TicTacToe object:</p>
  
  <pre><code>var theGame = new TicTacToe();
  </code></pre>
  
  <p>The following is my event for clicking a box on my grid:</p>
  
  <pre><code>// Column click event
  $(".col").click(function(){
  
      var row = $(this).parent().index();
      var col = $(this).index();
  
      if(theGame.checkIfPositionIsTaken(row, col) == false &amp;&amp;       theGame.checkIfGameHasEnded() == false){
  
  if(theGame.turn == theGame.player1Name){
  
      $(this).text(theGame.drawO(row, col));
  
      if(theGame.winnerCheck(1,theGame.player1Name) == false &amp;&amp; theGame.checkIfGameIsADraw() == false){
             theGame.turn = theGame.player2Name;
             theGame.setBoardMsg(theGame.player2Name+"'s turn now!");
          }
          return; 
      }
  else{
  
      $(this).text(theGame.drawX(row, col));
  
      if(theGame.winnerCheck(2,theGame.player2Name) == false &amp;&amp; theGame.checkIfGameIsADraw() == false){
             theGame.turn = theGame.player1Name;
             theGame.setBoardMsg(theGame.player1Name+"'s turn now!");
          }
          return; 
          }
  }
  });
  </code></pre>
  
  <p>This is my first question so just let me know if you need more information. Thanks.</p>