Braian Mellor
Braian Mellor

Reputation: 1954

JS Call function inside function run-time with string

I've found here is a good explication of the difference between run-time functions and parse-time ones. Bit what i'm trying to do is something like this

var funtionName = 'functionInside';
var start = function(){
    var a = function(){doSomething();}
    return {functionInside:a} 
};

And i want to call function 'functionInside' with the variable, something like

start.window[funtionName]()

Thanks in advance!

Upvotes: 1

Views: 238

Answers (1)

Benny Lin
Benny Lin

Reputation: 536

There are couple of ways to do that depending on what you need.

Here are two examples:

var start = {
  functionInside : function(){
    doSomething();    
  }
};

start[funtionName](); //different ways to invoke
start.functionInside();

Here's another approach:

var start = function() {
  this.functionInside = function() {doSomething();}
}

var s = new start();

s[funtionName](); //different ways to invoke
s.functionInside();

Upvotes: 1

Related Questions