I have problem with inheritance of consturctor:
function Alive(name) {
this.name = name;
}
var Cat = new Function();
Cat.prototype = new Alive();
Cat.prototype.constructor = Alive;
var cat = new Cat('Thomas');
alert(cat.name);
alert show undefined. What i do wrong? jsfiddle
Looks like you want the parent constructor to get called automatically, that’s not supported without some extra work. Your code should look like the following
If you use a library to implement inheritance, you don’t have to worry about this. They can even call your parent constructor automatically if you don’t want to create an empty constructor. The above code is still not ideal. See a post I wrote that talks about the ‘right’ way to do inheritance. http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html