could you please help me to solve this misconception about prototypal inheritance in JavaScript
function Animal(name) {
this.name = name;
}
Animal.prototype.speak = function() {
console.log("My name is " + this.name);
};
Animal.sayMoo = function(){
console.log("Mooooo!");
};
var animal = new Animal('Monty');
animal.speak(); // My name is Monty
animal.sayMoo; // undefined
as far as I know any instance of an object has all it properties, and in this example animal (instance of Animal) only has what inside the constructor "this.name" and the method "speek()" but not "sayMoo" because I did not use ( Animal.protoype.sayMoo = ...
I think maybe animal or any instance only inherit what is inside the Animal.prototype
is that the right answer? if yes, why animal inherits what inside the constructor ? maybe all properties inside the constructor go automatically to the prototype, so it can be inherited by any instance?
I wish i Solve this