0

in an existing implementation (can't change the structure much), i'm trying to call a function which is nested inside another function:

function outer(innerFunction, obj) {

    //TODO: call innerFunction here, passing obj as first parameter

    function inner1(obj) {
        alert(obj.key);
    }
}
outer('inner1', {key:'value'});

jsfiddle is here: http://jsfiddle.net/tbyyw/

i've alreay thought about using eval(), but i don't know how to pass an object - and they say 'eval is evil' ;)

another solution i've come up with is checking the innerFunction string, but this means i have to know which inner functions exist (besides, adding new functions would mean having to write extra checks then):

if(innerFunction == 'inner1') inner1(obj);

so is there another way without changing the overall implementation?

2
  • This question is hard to answer. What's the original implementation and wich parts of the overall implementation can be changed? From your code snippet it doesn't look like outer already exists. Commented Jul 2, 2012 at 20:42
  • the implementation already exists, so does the 'outer' function (i've just changed the identifiers to simplify things) Commented Jul 3, 2012 at 7:13

3 Answers 3

1

Without changing the overall structure eval appears to be the only option:

function outer(funcName, obj) {

    var func = eval(funcName);
    func(obj);

    function inner1(obj) {
        alert(obj.key);
    }
}

There's nothing particularly "evil" about eval as long as you have full control over the code, but if you want, you can insert an additional security check:

if (funcName.match(/\W/))
    throw "invalid function name!";

var func = eval(funcName);

This will raise an exception if someone tries to pass anything else than a simple identifier, i.e. a function name.

Sign up to request clarification or add additional context in comments.

2 Comments

seems like the best option, thanks for showing how to pass an object using 'eval' here.
Using eval can severely compromise 'minifying' (compression) algorithms of your script.
1

Is this what you wanted?

function outer(innerFunction, obj) {

    var fn = {
        inner1: function (obj) {
            alert(obj.key);
        } 
    };

    fn[innerFunction](obj);
}

outer('inner1', {key:'value'});

http://jsfiddle.net/tbyyw/1/

3 Comments

this means having to rewrite the 'inner' functions, which is not what i want
You are just renaming them, not rewriting them. The bottom line is there is no enumeration of local variables. So you have 3 options: 1) use eval. 2) switch statements or an index object. 3) put the variables in question into an enumerated object. I think 3 is best as it is safest.
you're right, it's no 'rewrite', i just don't want to touch the code block containing the inner functions at all - anyway, +1 for providing a viable solution
0

A simple switch statement would be least intrusive. Or is the function name completely dynamic?

function outer(innerFunction, obj) {

     switch (innerFunction) {
        case "inner1": inner1(obj); break;            
    }

    function inner1(obj) {
        alert(obj.key);
    }
}
outer('inner1', {key:'value'});
​

1 Comment

i've already mentioned this in my question (using a 'if' statement instead of 'switch')

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.