Visualizzazione dei risultati da 1 a 4 su 4

Discussione: Inheritance

  1. #1

    Inheritance

    Ciao.
    Seguendo questo tutorial ho trovato questo snippet:
    Codice PHP:
    <script language="JavaScript" type="text/JavaScript">
    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);
        }
    }  

    // derive a child object from DivA object
    function DivB(){
        
    // define object properties and override parent properties
        
    this.width='500px';
        
    this.height='200px';
        
    this.position='absolute';
        
    this.top='20px';
        
    this.left='20px';
        
    this.padding='10px';
        
    this.border='20px solid #0f0';
        
    this.backgroundColor='#00f';
    }
    DivB.prototype=new DivA();
    window.onload=function(){
    var 
    div2=new DivB();
    div2.display();

    }
    </script> 
    C'è un modo di ottenere la stessa cosa
    non utilizzando prototype.

    Sono anche graditi links


    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  2. #2
    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()

  3. #3
    Ciao.
    Ti ringrazio ma lo snippet era semplicemente
    a scopo didattico per approfondire come applicare
    l'ereditarietà a js e scoprire se esistono altri approcci senza
    l'utilizzo di prototype.


    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  4. #4
    ehehe allora mi sa che c'è poco da fare in quanto il concetto di eredità di javascript è proprio basato sul prototipo!
    Per dirla in altre parole NON c'è la parolina magica extends...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.