Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!