0

I am trying to call a function whose name is defined elsewhere is the code.

Below is sample code:

Inner.testFunc = function(node) { 
 var functionName = node.substring(node, to.indexOf('@'));
 // call the function based on functionName
 [functionName + '()']
}

function something() {
 //doing something
};

...

Assuming that functionName = something, this should try to call the function, but it isn't doing it.

I'm tried using other links such as Call javascript function which name is in variable but it didn't help.

I've also tried using eval():

eval(functionName + '()');

but this returned something about an illegal character...

Community
  • 1
  • 1
Gary
  • 395
  • 2
  • 7
  • 24

5 Answers5

3

Your eval call might not be working; use instead:

eval(functionName)(); //note () after the eval statement

In your code ensure first that functionName is something

console.log(functionName === 'something') //true

roland
  • 7,695
  • 6
  • 46
  • 61
2

If your function is defined in the global scope, you could take advantage of the fact that global variables and global functions can be accessed as properties of the global scope:

function something(){ console.log('Works!'); }
window['something'](); // Works!

If it is not, perhaps a reference to it is stored as a property of an object:

var x = { something : function(){ console.log('Also works'); } };
x['something'](); // Also works

Otherwise, it is impossible to access.

Tibos
  • 27,507
  • 4
  • 50
  • 64
  • window[] I've tried already and it doesn't work. Event trying to call it directly with window like you've done. I'll be honest, I don't know what is meant by it being in the Global scope, but I'm pretty sure it isn't... I'm running this in an external JS file not in the html one – Gary Jan 22 '14 at 15:29
  • being in the global scope means that the definition of the function is not inside another function. In this case, something is not in the global scope: `function() { function something(){} }`. It doesn't matter if the script is external or not. – Tibos Jan 22 '14 at 15:31
  • Ah I see, in that case it's not in the global scope, it's defined in a function: test.module(){ .... something(){...} .... } – Gary Jan 22 '14 at 15:55
0

functionName is the return of the substring method, not the function itself.

Harry
  • 52,711
  • 71
  • 177
  • 261
0

You can create a map of functions with their names as keys as shown below. And then you can pass the output of substring to the map and call the function.

function myName() {
    console.log("thefourtheye");
}

var functions = {             // map of functions and their names
    name: myName
};

functions["name"]();          // Instead of "name" pass the result of substring
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

It's a little more verbose but what about using a case statement to run a function based on the value of node.

Paul Brindley
  • 273
  • 1
  • 2
  • 13
  • Well this is a slightly simplified version, but later in the code I am cutting three variables to build the function name so it would be a bit too complicated then – Gary Jan 22 '14 at 15:35
  • You're not making it easy on yourself are you! – Paul Brindley Jan 22 '14 at 15:37