Javascript Object Inheritance via Prototype

Description

Javascript Object Inheritance via Prototype



function Animal(group, gender) {
  this.group = group;/* w w w.  j ava  2s.  c  o  m*/
  this.gender = gender;
}
Animal.prototype = {
    group: "",
    gender: "",
    eat: function() {
        return "eat";
    },
    sleep: function() {
        return "zzzzzzzz..";
    }
}

function Bird(gender) {
  Animal.call(this,"bird", gender);
}
Bird.prototype = new Animal();
Bird.prototype.fly = function() {
    return "fly!";
}

Bird.prototype.constructor = Bird;

let flamingo = new Bird();

console.log(flamingo.eat());



PreviousNext

Related