constructors and inheritance in JS
Posted
by nandinga
on Stack Overflow
See other posts from Stack Overflow
or by nandinga
Published on 2010-03-19T17:33:59Z
Indexed on
2010/03/19
23:11 UTC
Read the original article
Hit count: 389
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!!
© Stack Overflow or respective owner