I'm studying inheritance in javascript and I stumbled upon an issue regarding sharing prototypes functions. Here is my code that works:
function basicID(id,firstname,lastname){
this.firstname=firstname;
this.lastname=lastname;
}
basicID.prototype = {
setFirstName:function(firstname){
this.firstname=firstname;
},
setLastName:function(lastname){
this.lastname=lastname;
},
getFirstName:function(){
return this.firstname;
},
getLastName:function(){
return this.lastname;
}
};
If test this by
var empID = new basicID();
empID.setFirstName("Pink Panther");
alert(empID.getFirstName());
It works well.
Then I wanted to create a subclass of this basicID using the following..
function advancedID(id,firstname,lastname,address,picture,accesscode){
basicID.call(this,id,firstname,lastname);
this.address=address;
this.picture = picture;
this.accesscode= accesscode;
}
advancedID.prototype = basicID.prototype;
Then test this by...
var employeeID = new advancedID();
employeeID.setFirstName("Black Panther");
alert(employeeID.getFirstname())
And still works! But when i added the prototype functions such as this:
advancedID.prototype = {
setAddress : function(address){
this.address = address;
},
getAddress : function(address){
return this.address;
}
}
Then I test the advanceID's own function with
employeeID.setAddress("Metropolis");
alert(employeeID.getAddress())
and it didn't work! I suspect the line
advancedID.prototype = basicID.prototype;
For the errors. Since It changes the prototype object of that particular object. How can I inherit both functions from the basicID and advancedID at the same time?