-2

I am working inheritance in JavaScript, here, have created two objects. on the below code, when try to alert as alert(newOnj.whatAreYou());, now, it is working fine. But, when i try to alert alert(newOnj.whatAreYounow());, now, am getting error as 'newOnj.whatAreYounow is not a function'. Actually, have inherited parent class to child class as subGadget.prototype = new Gadget();. what is the thing that i haven't understood here? please

function Gadget() { 
   this.name = "Alex"; 
   this.color = "red"; 
   this.whatAreYou = function(){ 
     return 'I am a ' + this.color + ' ' + this.name; 
   }
}
  function subGadget() { 
     this.whatAreYounow = function(){ 
     return 'Now, I am a ' + this.color + ' ' + this.name; 
   }
}

  subGadget.prototype = new Gadget();

  var newOnj = new Gadget();

 alert(newOnj.whatAreYounow());
Sakthivel
  • 678
  • 2
  • 6
  • 19

1 Answers1

-1

You need to write -

var newOnj = new subGadget();
  • 1
    … and he would need to [inherit correctly](http://stackoverflow.com/q/10898786/1048572) from `Gadget`. – Bergi Dec 09 '14 at 19:17