Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
Javascript class method reference by varible

Is it possible to reference a method by a variable?

What I would like to do it pretty much to make a wrapper class for a db table which can update an HTML element when changed, pretty much the same as you can do in XAML. It's easy if you do one property a time but after load you may want to loop over all properties.

example:

class A {
   constructor() {
        this.__element = {
            ID: [],
            Name: [],
            OwnerName: [],
            OwnerID: [],
            Comment: []
        };
    }

    get Name() { return this._name; }

    // More property methods [...]

    AssignElement(Field, ElementID, TargetProperty) {
        const obj = {Name: ElementID, Target: TargetProperty};
        this.__element[Field].push(obj);
    }

    _setElement(property, value) {
        this.__element[property].forEach(v => {
            if ((v.Target).toLowerCase() == 'value') {
                document.getElementById(v.Name).value = value;
            } else {
                document.getElementById(v.Name).innerHTML = value;               
            }
        });
    }

  loopThrough() {
    for (const key in this.__element) {
        // Okay this will of course not work but you get the idea..
        this._setElement(key, this.[key]);
    }
  }

}

Is this approach possible or is there an easier and better approach?

about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda