While going through javascript course on codecademy.com, I've become slightly confused.
So first we've been learning how to add method to a Class:
function Dog (breed) {
this.breed = breed;
this.sayHello = function () {
console.log("Hello this is a " + this.breed + " dog");
}
};
var someDog = new Dog("golden retriever");
someDog.sayHello();
Then we started the "prototype". And there was this example:
function Dog (breed) {
this.breed = breed;
};
Dog.prototype.sayHello = function () {
console.log("Hello this is a " + this.breed + " dog");
}
var someDog = new Dog("golden retriever");
someDog.sayHello();
Both examples are giving the same result. Are these two examples are just two ways doing same thing? Or there is a practical difference between two of them?