Javascript inheritance with basic example

Using prototype it is possible to implement inheritance in Javascript. Following is the code snippet of a Javascript prototypical inheritance.
var Animal = function(animalName) {
  this.name    = (animalName) ? animalName : 'unknown';
  this.getName = function () {
    return this.name;
  }
  return this;
};

var Cow = function () {
  this.name      = "Cow";
  this.getSounds = function () {
    return 'Moo';
  }
  return this;
};

Cow.prototype = new Animal('aa');
var cowObj    = new Cow();

console.log(cowObj.getSounds());
console.log(cowObj.getName());

Comments