2

Suppose I have the following JavaScript class code. (EDIT: the code is running as an ES6 module that is imported using the standard ES6 "import" statement. So the following class is in a standalone file "C.js")

export class C {
    static f() {
        console.log("hello world")
    }
}

Now I want to import the module and invoke the method C.f(), but I only have the name of the method as a string. The following works fine:

import {C} from "C.js"
var methodName="f"
C[methodName]()

Now suppose that I also have the name of the class as a string as follows:

import {C} from "C.js"
var className="C"
var methodName="f"
/* How do I invoke C.f() ? */

Things I've tried:

  • Using window/self/globalThis, which do NOT work:
import {C} from "C.js"
var className="C"
var methodName="f"
/* none of the below calls work */
window[className][methodName]()
self[className][methodName]()
globalThis[className][methodName]()

I've tried using the Function() constructor:

import {C} from "C.js"
var className="C"
var methodName="f"
/* the following does NOT work: */
var fn = new Function(className+"."+methodName+"()")
fn()

The above code gives an error that "C" is not defined, when the fn() is executed.

I've also tried using "eval()", which would be my last choice anyways since I don't really need to evaluate "arbitrary" code. I just want to execute a static method in a class, whose names are in strings.

import {C} from "C.js"
var className="C"
var methodName="f"
var code = className + "." + methodName + "()"
eval(code)

The above does NOT work in the Chrome web browser as part of a modern ES6 JavaScript application. The problem in the Chrome web browser is that the global "environment" used within "eval" does NOT see the "C" class definition.

So, what is the best way to implement this very simple call using string values for the class name and method name?

import {C} from "C.js"
var className="C"
var methodName="f"
/* now what? */
20
  • 3
    Then there's no built-in way to access the name dynamically. Add all your classes to an object and use that. Commented Feb 23, 2022 at 19:21
  • 2
    myClasses = {"C": C} then use myClasses[className] Commented Feb 23, 2022 at 19:21
  • 4
    Variable variables are almost always the wrong solution for something. Commented Feb 23, 2022 at 19:22
  • 1
    To be fair, the call to the JavaScript function call is originating from a Lua script, whose interpreter is running inside an Ionic capacitor plugin. This whole thing runs as part of a mobile app in Android. Commented Feb 23, 2022 at 19:23
  • 1
    In Class C, Class should be all lowercase. I'm not sure how you're code worked. Commented Feb 23, 2022 at 19:26

1 Answer 1

1

Since the answer is buried in the comments to the question, I figured I should go ahead and post it here as an actual answer, so future viewers can easily find the answer.

The answer is: In JavaScript, it's not possible to invoke a method in a class that was loaded from an ES6 module, where the name of the class is in a string.

Sign up to request clarification or add additional context in comments.

2 Comments

Gotta love stack overflow!
Worth adding the workaround as well to the answer from @barmar: myClasses = {"C": C} then use myClasses[className]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.