// global list of persons
var pList=[];
// consider person as an abstract klasse
function Person(){
this.navn;
this.faar;
this.getNavn=function(){return this.navn}
this.getAlder=function(){
return new Date().getFullYear()-this.faar
}
}
function Student(navn,faar,linje){
//console.log(Object.getPrototypeOf(this));
this.navn=navn;
this.faar=faar;
this.linje=linje;
this.getLinje=function(){return this.linje};
}
function Professor(navn,faar,fakultet){
this.navn=navn;
this.faar=faar;
this.fakultet=fakultet
this.getFakultet=function(){return this.fakultet}
}
function displayList(dumpid){
var dmp=document.getElementById(dumpid);
S="";
for(var ix=0;ix < pList.length;ix++){
var p=pList[ix];
// all appears as type of person
// we differentiate with
// test om method existence
S+=p.getNavn();
if(p.getLinje)
S+=", student "+p.getLinje();
if(p.getFakultet)
S+=", ansatt ved "+p.getFakultet();
S+=", alder: "+p.getAlder();
S+="<br/>";
}
dmp.innerHTML=S;
}
Så lager vi en serie objekter.
var pers=new Person();
Professor.prototype=pers;
Student.prototype=pers;
// legal, but with undefines attributes
// pList.push(new Person("Jensen",2001));
pList.push(new Student("Pedersen",1985,"Informatikk"));
pList.push(new Student("Hansen",1990,"Matematikk"));
pList.push(new Professor("Berg",1970,"Avdeling for IT"));
pList.push(new Student("Holm",1990,"Økonomi"));