title: Creating a simple JavaScript class with jQuery
body: <p>I'm trying to understand jQuery classes but it is not going very well.</p>
  
  <p>My goal is to use a class this way (or to learn a better way to do it):</p>
  
  <pre><code>var player = new Player($("playerElement"));
  player.InitEvents();
  </code></pre>
  
  <p>Using other people's examples, this is what I tried:</p>
  
  <pre><code>$.Player = function ($) {
  
  };
  
  $.Player.prototype.InitEvents = function () {
  
      $(this).keypress(function (e) {
          var key = e.which;
          if (key == 100) {
              MoveRight();
          }
          if (key == 97) {
              MoveLeft();
          }
      });
  };
  
  $.Player.prototype.MoveRight = function () {
      $(this).css("right", this.playerX += 10);
  }
  
  $.Player.prototype.MoveLeft = function () {
      $(this).css("right", this.playerX -= 10);
  }
  
  $.Player.defaultOptions = {
      playerX: 0,
      playerY: 0
  };
  </code></pre>
  
  <p>The end goal is to have a character moving on the screen left and right using the keyboard letters <code>A</code> and <code>D</code>.</p>
  
  <p>I have a feeling that I'm doing something very wrong with this "class"
  but I'm not sure why.</p>
  
  <p>(sorry for my English)</p>