constructors and inheritance in JS
- by nandinga
Hi all,
This is about "inheritance" in JavaScript.
Supose I create a constructor Bird(), and another called Parrot() which I make to "inherit" the props of Bird by asigning an instance of it to Parrot's prototype, like the following code shows:
function Bird() {
this.fly = function(){};
}
function Parrot() {
this.talk = function(){ alert("praa!!"); };
}
Parrot.prototype = new Bird();
var p = new Parrot();
p.talk(); // Alerts "praa!!"
alert(p.constructor); // Alerts the Bird function!?!?!
After I've created an instance of Parrot, how comes that the .constructor property of it is Bird(), and not Parrot(), which is the constructor I've used to create the object?
Thanks!!