Lets say I have a class:
function Dog(name) {
this.name = name;
}
Dog.prototype = {
constructor: Dog
}
I would like to add some methods to that class, but I'm confused where I should add them, to the constructor itslef, or to the prototype of that class.
for example I can do:
function Dog(name) {
this.name = name;
this.bark = function() {
console.log(this.name + ' barked!');
}
}
Or I can alternatively do this:
function Dog(name) {
this.name = name;
}
Dog.prototype = {
constructor: Dog,
bark: function() {
console.log(this.name + ' barked');
}
}
What are the differences between the 2 options? which one is better? and which situations should I use each one?
Sidenote: I am aware this question is basic enough to likely have duplicates, but I cannot find any simillar posts, maybe I am constructing the question wrong.