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
}
}