0

I have this object in JS:

var persona = { 
     nome: "",
     cognome: "",
     sesso: "",
     telefono: "",
     indirizzo: {
         via: "",
         numero: "",
         CAP: ""
         },
     nascita: {
        mese: "",
        giorno: "",
        anno: "",
        CAP: ""
        },
    codiceFiscale: function() 
       {
       // istruzioni per il calcolo
       },
    input: function(name,surname,sex,street,number,CAP1,day,month,year,CAP2)
      {
      // istruzioni
      }
 };

I would like to transform it in a constructor Persone(), so I can use it to declare an array, something like this:

  var archivio = new Array();
  archivio.push(new Persone());

How can I do? I am able to do it without nested objects, but here I am quite confused. Thanks a lot in advance!

4 Answers4

1

Within your Persone constructor function, you'd assign nested objects to their properties on this (which refers to the new object) using the same kind of notation you're using now:

function Persone() {
    this.nome = "";
    this.cognome = "";
    this.sesso = "";
    this.telefono = "";
    this.indirizzo = {
        via: "";
        numero: "";
        CAP: ""
    };
    this.nascita = {
        mese: "";
        giorno: "";
        anno: "";
        CAP: ""
    };
    this.codiceFiscale = function() {
        // istruzioni per il calcolo
    };
    this.input = function(name, surname, sex, street, number, CAP1, day, month, year, CAP2) {
        // istruzioni
    };
}

Naturally, you can add parameters to Persone (for nome, etc.) if you like and use those when creating the properties on this.

You might consider moving the functions off to the prototype that new Persone will assign to new instances (unless they need to be different for different Persone instances), like this:

function Persone() {
    this.nome = "";
    this.cognome = "";
    this.sesso = "";
    this.telefono = "";
    this.indirizzo = {
        via: "";
        numero: "";
        CAP: ""
    };
    this.nascita = {
        mese: "";
        giorno: "";
        anno: "";
        CAP: ""
    };
}
Persone.prototype.codiceFiscale = function() {
    // istruzioni per il calcolo
};
Persone.prototype.input = function(name, surname, sex, street, number, CAP1, day, month, year, CAP2) {
    // istruzioni
};

It's also worth looking at the newish class syntax, which you can use today with transpiling (or, increasingly, without):

class Persone {
    constructor() {
        this.nome = "";
        this.cognome = "";
        this.sesso = "";
        this.telefono = "";
        this.indirizzo = {
            via: "";
            numero: "";
            CAP: ""
        };
        this.nascita = {
            mese: "";
            giorno: "";
            anno: "";
            CAP: ""
        };
    }

    codiceFiscale() {
        // istruzioni per il calcolo
    }

    input(name, surname, sex, street, number, CAP1, day, month, year, CAP2) {
        // istruzioni
    }
}

Finally, if you want to create those nested objects with constructors as well, you'd just do that and then use the constructors within Persone:

class Indirizzo {
    constructor() {
        this.via = "";
        this.numero = "";
        this.CAP = "";
    }
}

class Nascita {
    constructor() {
        this.mese = "";
        this.giorno = "";
        this.anno = "";
        this.CAP = "";
    }
}

class Persone {
    constructor() {
        this.nome = "";
        this.cognome = "";
        this.sesso = "";
        this.telefono = "";
        this.indirizzo = new Indirizzo();
        this.nascita = new Nascita();
    }

    codiceFiscale() {
        // istruzioni per il calcolo
    }

    input(name, surname, sex, street, number, CAP1, day, month, year, CAP2) {
        // istruzioni
    }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

ES6 (using Classes):

class Persone {
        constructor() {
            this.nome = "";
            this.cognome = "";
            this.sesso = "";
            this.telefono = "";
            this.indirizzo = {
                via: "";
                numero: "";
                CAP: ""
            };
            this.nascita = {
                mese: "";
                giorno: "";
                anno: "";
                CAP: ""
            };
        }

        codiceFiscale() {
            // istruzioni per il calcolo
        }

        input(name, surname, sex, street, number, CAP1, day, month, year, CAP2) {
            // istruzioni
        }
    }
0
function Persona(nome, cognoma) {
  this.nome = noma;
  this.cognoma = cognoma;
}

var persona = new Persona('test', 'test2');
persona.nome // will equal test
persona.cognoma // will equal test2

This should do it.

Roy Berris
  • 1,502
  • 1
  • 17
  • 40
0

Just pass the data into the constructor function when initializing.

var Persona = function(data){
  this.data = { 
     nome: data.nome,
     cognome: data.cognome,
     sesso: data.sesso,
     telefono: data.telefono,
     indirizzo: data.indirizzo,
     nascita: data.nascita
 };
}

Persona.prototype =  {
   codiceFiscale: function(){     
       //data object is accessible here as
       //this.data.nome...
   },
   input: function(name,surname,sex,street,number,CAP1,day,month,year,CAP2){
      // istruzioni
   }
}

var arr = new Array();

arr.push(new Persona({
   nome: "nome1",
   cognome: "cognome1",
   sesso: "sesso1",
   telefono: "telefono1",
   indirizzo: {},
   nascita: {}
}));

arr.push(new Persona({
   nome: "nome2",
   cognome: "cognome2",
   sesso: "sesso2",
   telefono: "telefono2",
   indirizzo: {},
   nascita: {}
}));


console.log(arr);
Thusitha
  • 3,393
  • 3
  • 21
  • 33