0

I found some questions related to my question, but none of the answers solved my problem for some reason. I want to call a method in javascript dynamically from a variable.

This is my code:

var malt_type;

export function json_handler(type){
    malt_type = type;

    try {
        window[malt_type]();
    } catch (e) {
        console.log(e);
    }
}

function SQL(){
    console.log(malt_type);
}

I tried this also, but didn't work either

var malt_type;

export function json_handler(type){
    malt_type = type;
    try {    
        var fnName = malt_type + "()";
        var fn = new Function(malt_type);
        console.log(fn);
        fn();
    } catch (e) {
        console.log(e);
    }
}

function SQL(){
    console.log(malt_type);
}

In the previous one the console.log(fn); writes this on the DevTools:

ƒ anonymous(
) {
SQL()
}

And the error I catch is like this (SQL is the content of the variable):

ReferenceError: SQL is not defined
    at eval (eval at json_handler (json_handler.js:11), <anonymous>:3:1)
    at Module.json_handler (json_handler.js:13)
    at Show (server.js:39)
    at Socket.<anonymous> (server.js:20)
    at Socket.emit (events.js:314)
    at TCP.<anonymous> (net.js:672)

My reason is that I want to do this is because I want to handle logs and the type of them differently.

5
  • In the example that "works" without error, you are printing the function, which is an object. You are not executing the function. To execute a function, you need (). Commented Nov 15, 2020 at 19:04
  • I execute with this in the code: fn(); And i put '()' in the variable Commented Nov 15, 2020 at 19:07
  • What I want is to run the function SQL from a variable Commented Nov 15, 2020 at 19:09
  • How do you call json_handler? Commented Nov 15, 2020 at 19:12
  • From another JS, but I get the argument correctly, thats not the problem I think. If I print out the variable I get the SQL(), the only problem I can't execute it correctly. Commented Nov 15, 2020 at 19:13

2 Answers 2

2

Functions created with the Function constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the Function constructor was created.

https://developer.mozilla.org/.../Function

The above basicaly means the fn function doesn't have access to SQL variable.

You could try it like this:

var malt_type;

export function json_handler(type){
    malt_type = type;

    // if it exists execute it
    functions[type] && functions[type]();
}

const functions = {
    SQL: function SQL(){
        console.log(malt_type);
    }
};
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it as illustrated below.

var malt_type;

function json_handler(type){
    malt_type = type;
    try {    
       window[malt_type](); //assuming your SQL() function is defined on the `window` object
    } catch (e) {
       console.log(e);
    }
}

function SQL(){
    console.log(`${malt_type} function ran.`);
}

json_handler("SQL");

1 Comment

I put export here: export function json_handler but I get: TypeError: window[malt_type] is not a function. I call the function like this: import * as json_handler from "./json_handler.js"; json_handler.json_handler(data2.malt_type);

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.