title: Overriding methods in Javascript
body: <p>I would like to know what is the difference between overriding methods with prototypes and without prototypes. Consider:</p>
  
  <p><strong>Example 1:</strong></p>
  
  <pre><code>function Animal() {
      this.sleep = function () {
          alert("animal sleeping");
      };
  
      this.eat = function () {
          alert("animal eating");
      };
  }
  
  function Dog() {
      this.eat = function () {
          alert("Dog eating");
      };
  }
  
  Dog.prototype = new Animal;
  
  var dog = new Dog;
  
  dog.eat();
  </code></pre>
  
  <p><strong>Example 2:</strong></p>
  
  <pre><code>function Animal() { }
  
  function Dog() { }
  
  Animal.prototype.sleep = function () {
      alert("animal sleeping");
  };
  
  Animal.prototype.eat = function () {
      alert("animal eating");
  };
  
  Dog.prototype = new Animal;
  
  Dog.prototype.eat = function () {
      alert("Dog eating");
  };
  
  var dog = new Dog;
  
  dog.eat();
  </code></pre>
  
  <p>I feel both examples produce the same effect that the <code>Dog</code> class is overriding the eat method of the <code>Animal</code> class. Or is there anything different happening?</p>