This appears to be a good way to do inheritance:
//--------- SuperType definition ------------
function SuperType(name){
this.name = name;
this.nickName = "None";
this.colors = ["red", "blue", "green"];
}
SuperType.prototype.setNickname = function(nickName){
this.nickName = nickName;
}
SuperType.prototype.sayName = function(){
alert("Name = "+this.name+", Nickname = "+this.nickName);
};
//--------- SubType definition ------------
function SubType(name, age){
SuperType.call(this, name);
this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.sayAge = function(){
if(this.nickName == "None"){
alert(this.name+" is "+this.age+" years old");
}else{
alert(this.nickName+" is "+this.age+" years old");
}
};
//----------- sequential code --------------------
var instance1 = new SubType("Nicholas", 29);
instance1.setNickname("Nick");
instance1.colors.push("black");
alert(instance1.name+"'s colors are: "+instance1.colors); //"red,blue,green,black"
instance1.sayName(); //"Nicholas";
instance1.sayAge(); //29
var instance2 = new SubType("Greg", 27);
alert(instance2.name+"'s colors are: "+instance2.colors); //"red,blue,green"
instance2.sayName(); //"Greg";
instance2.sayAge(); //27