Certo che c'è!
Visto che la funzine DivB() ha il solo scopo di fare un overriding possiamo tranquillamente eliminarla.
Nella funzione DivA invece gli passiamo tanti argomenti quanti sono le prorietà associate all'istanza dell'oggetto implicito this es:
codice:
function DivA(){
// define object properties
this.div=document.createElement('div');
this.width='400px';
this.height='300px';
this.position='absolute';
this.top='10px';
this.left='10px';
this.padding='5px';
this.border='1px solid #000';
this.backgroundColor='#f00';
// define 'display()' method
this.display=function(){
this.div.style.width=this.width;
this.div.style.height=this.height;
this.div.style.position=this.position;
this.div.style.top=this.top;
this.div.style.left=this.left;
this.div.style.padding=this.padding;
this.div.style.border=this.border;
this.div.style.background=this.backgroundColor;
document.getElementsByTagName('body')[0].appendChild(this.div);
}
}
diventa :
codice:
function DivA(w,h,pos,t,l,pad,bord,back){
// define object properties
this.div=document.createElement('div');
this.width=w;
this.height=h;
this.position=pos;
this.top=t;
this.left=l;
this.padding=pad;
this.border=bord;
this.backgroundColor=back;
// define 'display()' method
this.display=function(){
this.div.style.width=this.width;
this.div.style.height=this.height;
this.div.style.position=this.position;
this.div.style.top=this.top;
this.div.style.left=this.left;
this.div.style.padding=this.padding;
this.div.style.border=this.border;
this.div.style.background=this.backgroundColor;
document.getElementsByTagName('body')[0].appendChild(this.div);
}
}
ovviamente nella chiamata bisogna stare attenti a passargli i parametri giusti, ovvero ad es:
var obj=new DivA("400px","300px","absolute","10px","10px","5px ","1px solid #000","#f00")
obj.display()