How does this syntax Penguin.prototype = new Animal();
inherit properties from constructor Animal
. It also uses the "new" keyword, why does it use that? as far as I know, it is used to create objects.
Things that I know -There is actually no "class" in JavaScript. Every constructor/class or object has a prototype. We use "prototype" keyword to add properties or methods to it. We also use it to inherit properties from constructors, like in the above code. We can add methods or properties to object using dot operator, but we can't do that with constructors.
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
var Penguin = function(name) {
this.name = name;
this.numLegs = 2;
}
Penguin.prototype = new Animal("lavios",89);