Im curently learning about the prototype. Is it better to put the function "sayName" in the class or add it later via a prototype? or is it the same and depends on the situation?
function Animal(name,numLegs){
this.name = name;
this.numLegs = numLegs;
this.sayName = function(){
console.log("Hi my name is " + this.name);
};
}
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();
or
function Animal(name,numLegs){
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function(){
console.log("Hi my name is " + this.name);
};
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();