2017-09-18 8 views
0

私はJSでこのオブジェクトを持っているため、コンストラクタ:Javascriptを:ネストされたオブジェクト

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

私はコンストラクタPersone()でそれを変換したいと思いますので、私は配列を宣言するために使用することができ、このような何か:

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

どうすればいいですか?私はネストされたオブジェクトなしでそれを行うことができますが、ここで私はかなり混乱しています。ありがとうございます! 、当然

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

:あなたのPersoneコンストラクタ関数内で

+2

代わりにオブジェクトリテラルのコンストラクタ関数を使用しますか? – Li357

+0

'関数Persone(){this.nome = ''; this.indirizzo = {via: ''}} ' – dfsq

答えて

1

は、あなたが今使用している表記の同じ種類を使用して(新しいオブジェクトを指している)thisにそのプロパティにネストされたオブジェクトを割り当てると思いますあなたが好きな場合はPersonenomeなどの場合)にパラメータを追加し、thisにプロパティを作成するときにパラメータを使用できます。

あなたはこのように、(それらが異なるPersoneインスタンスに異なる必要がない限り)new Personeが新しいインスタンスに割り当てるプロトタイプを切る機能を移す検討するかもしれない:

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

また見て価値があります

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

最後に、あなたが同様にコンストラクタを持つものネストされたオブジェクトを作成する場合は、あなただけの思い:あなたは(せずに、ますますまたは、)transpilingで今日を使用することができ、class syntax新しめそれを行うと、その後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 
    } 
} 
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 

これはそれを行う必要があります。 (Classesを使用して)

1

ES6:

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

初期化時だけコンストラクタ関数にデータを渡します。

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);

関連する問題