0

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?

0 Answers0