Overriding methods in Javascript
I would like to know what is the difference between overriding methods with prototypes and without prototypes. Consider: Example 1: 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(); Example 2: function Animal() { } function Dog() { } Animal.prototype.sleep = function () { alert("animal sleeping"); }; Animal.prototype.eat = function () { alert("animal eating"); }; Dog.prototype = new Animal;