【问题标题】:Javascript: constructor for nested objects [duplicate]Javascript:嵌套对象的构造函数[重复]
【发布时间】:2018-02-27 00:29:02
【问题描述】:

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

我想在构造函数 Persone() 中对其进行转换,这样我就可以使用它来声明一个数组,如下所示:

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

我该怎么办?我可以在没有嵌套对象的情况下做到这一点,但在这里我很困惑。提前非常感谢!

【问题讨论】:

  • 使用构造函数而不是对象字面量?
  • function Persone () { this.nome = ''; this.indirizzo = { via: '' } }

标签: javascript


【解决方案1】:

在您的Persone 构造函数中,您可以使用您现在使用的相同符号将嵌套对象分配给它们在this(指新对象)上的属性:

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(用于nome 等)并在this 上创建属性时使用这些参数。

您可能会考虑将函数移至 new Persone 将分配给新实例的原型(除非它们对于不同的 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 syntax,您现在可以将其与转译一起使用(或者越来越多地不使用):

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

最后,如果你也想用构造函数创建那些嵌套对象,你只需这样做,然后在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
    }
}

【讨论】:

    【解决方案2】:
    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
    

    应该这样做。

    【讨论】:

      【解决方案3】:

      ES6(使用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
              }
          }
      

      【讨论】:

        【解决方案4】:

        初始化时只需将数据传入构造函数即可。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-03
          • 2023-03-13
          • 2012-12-22
          • 1970-01-01
          • 2018-05-09
          • 1970-01-01
          • 2012-09-21
          • 1970-01-01
          相关资源
          最近更新 更多