Prototype animal/penguin


#1



Its not behaving incorrectly. I just have a question. Can someone explain to me in detail how the computer is thinking step by step when penguin.sayName(); is called. I just want to understand so I can write my own code without guessing what the computer is thinking. Thank You in advance.


R//prototype animal/penguin

function Animal (name, numLegs){
	this.name = name;
	this.numLegs = numLegs;
}
Animal.prototype.sayName = function(){
	console.log("Hi my name is " + this.name);
};
function Penguin (name){
	this.name = name;
	this.numLegs = 2
};
Penguin.prototype = new Animal();
var penguin = new Penguin("Frank");
penguin.sayName();


#2

we have two objects:

animal and penguin

penguin is an animal, so it inherits from Animal.

then we create an instance, a penguin called frank:

var penguin = new Penguin("Frank");

now we can call a method:

penguin.sayName();

sayName is a animal method, but given Penguin inherits from Animal, we can also call parent classes methods


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.