Farshad Fahimi
Farshad Fahimi

Reputation: 648

Run function dynamically in es6

I have a file with list of function like below

export function today() {}

export function yesterday() {}

export function tomorrow() {}

export function out(input) { input() }

input is the name of the function and it could be one of tomorrow, today or yesterday functions.

I just need to dynamically call today, yesterday, tomorrow functions.

Upvotes: 2

Views: 1966

Answers (4)

Rajesh
Rajesh

Reputation: 24955

As commented, you will have to create a map and using that you can call the functions.

You can try one of following approach:

Single export

Benefit of this approach is that you have a single export, so you know what all you are exporting. However, you will have to scroll up and down to see definition, if the file grows big. Plus, you are bleeding everything, so out(...) can call any function, so no point in exporting them individually

function today() {}
function yesterday() {}
function tomorrow() {}
function out(input) {
  module[input]();
}

var module = {
  today, yesterday, tomorrow, out
}

export module;

Map of possible function

This is more recommendable approach. Here you have a map of possible actions. So, you are restricting possible inputs as well. This will also enable you to have more meaningful names for both action and function. Disadvantage would be to have to maintain a list. So for every new action, you will have to update this list

export function today() {}
export function yesterday() {}
export function tomorrow() {}

const actionMap = {
  TODAY: today,
  YESTERDAY: yesterday,
  TOMORROW: tomorrow
}

export function out(input) {
  actionMap[input]();
}

Upvotes: 2

Moritz Roessler
Moritz Roessler

Reputation: 8651

You basically have two options. Do a lookup using a map or move the referenced functions into another file and do a wildcard import from there. (which is essentially the same)

1. With a lookup

const lookup = {today, yesterday, tomorrow}
export function today() {}
export function yesterday() {}
export function tomorrow() {}
export function out(input) { lookup[input]() }

2. Split your code into two files. index.js and fns.js
index.mjs

import * as lookup from './fns'
export function out(input) { lookup[input]() }  

fns.mjs

export function today() {}
export function yesterday() {}
export function tomorrow() {}

Upvotes: 1

Rubin bhandari
Rubin bhandari

Reputation: 1951

function func_abc() {
    alert("abc");
}
function call_others(function_name) {
    if (function_name == "abc") {
        func_abc();
    }
}
call_others("abc");

You can do this or use switch statement

Also one hack is to store such functions in variables and then call them by accessing them from the module object properties. Example:

var x = { }; 
x.f1 = function()
{
    console.log('Call me as a string!');
}

Now, you can call it using the value from a string:

var funcstr = "f1";
x[funcstr]();

Upvotes: 0

Rubin bhandari
Rubin bhandari

Reputation: 1951

function func_abc() {
    alert("abc");
}
function call_others(function_name) {
    if (function_name == "abc") {
        func_abc();
    }
}
call_others("abc");

You can do this or use switch statement

Upvotes: 0

Related Questions