Calling a static method by name as string in JavaScript

1

If I want to call a static function by name as string, how do I?

I did what is below, suppose the function name is "move".

class Transformation {
    static operate(object, function_name) {
          eval(function_name)(object);
    }
    static move(object) {... some code...}
}

But it did not work.

NOTE: I'm calling this function from another file in the same directory.

    
asked by anonymous 25.07.2018 / 01:40

1 answer

2

I do not know if I understood correctly what you want, but maybe that's it:

class Transformation {
    static operate(function_name, object) {
        Transformation[function_name](object);
    }
    static move(object) { alert("oi " + object); }
}

Transformation.operate("move", "teste");
    
25.07.2018 / 01:53