giovedì 1 maggio 2014

Un costruttore completo in JavaScript

Ed ecco un costruttore completo, con proprietà e un metodo:
function Oggetto(){
 this.nome="Antonello";
 this.cognome="Iaccarino";
 this.StampaNome=function(){
  alert(this.nome + " " + this.cognome);
 }
}
var Istanza=new Oggetto();
Istanza.StampaNome();
Funziona egregiamente!
Per meglio copiare e incollare i risultati, invece di alert uso document.write.
function Oggetto(){
 this.nome="Antonello";
 this.cognome="Iaccarino";
 this.StampaNome=function(){
  document.write(this.nome + " " + this.cognome);
 }
}
var Istanza=new Oggetto();
Istanza.StampaNome();
Antonello Iaccarino 


Nessun commento:

Posta un commento