JavaScript - Inheritance, inherit properties and methods from parent class

1.Penguin class will inherit properties and classes from Animal class

1:  // the original Animal class and sayName method  
2:  function Animal(name, numLegs) {  
3:    this.name = name;  
4:    this.numLegs = numLegs;  
5:  }  
6:  Animal.prototype.sayName = function() {  
7:    console.log("Hi my name is " + this.name);  
8:  };  
9:  // define a Penguin class  
10:  function Penguin(name){  
11:    this.name = name;  
12:    this.numLegs = 2;  
13:  }  
14:  // set its prototype to be a new instance of Animal  
15:  Penguin.prototype = new Animal();  

No comments:

Post a Comment