A call to a nested function when a function name is passed as a string

I am trying to access a nested function by passing the function name as a string and then calling it. For example, see this post

function outer(action){
    window["outer"][action]();

    function inner(){
        alert("hello");
    }
}
outer("inner");

However, this will not work. Error:

window.outer[action] is not a function

How to make this work or an alternative way to call a nested function.

The reason for this is because I am trying to hide a bunch of functions called iframes inside the function area.

+5
source share
2 answers
function outer(action){
   var inner = {
     func1: function() {},
     func2: function() {},  
     func3: function() {},
     // ...
   }
   inner[action]();
}

outer("func1");
+6
source

Thus, you are trying to access the "internal" property of the "external" function (outer.inner), which is not defined. The only way to do this is to use eval:

function outer(action){
    eval(action+"()");

    function inner(){
        alert("hello");
    }
}
outer("inner");

, eval - , , .

+1

All Articles