0

I have a function like this.

function myfun(input1, input2, input3){
    //
    // Other codes which deals with input2 and input3 (irrelevant)
    //
    function a(){
        console.log('func a');
    }
    function b(){
        console.log('func b');
    }
    function c(){
        console.log('func c');
    }
    function d(){
        console.log('func d');
    }
    // input1 is an array
    for(var i=0; i<input1.length; i++){
        var name = input1[i];
        name(); // Doesn't work as 'name' refers to a string;
        this[name](); // Doesn't work;
        // How can i execute the nested function whose name is stored in input array ?
    }
}
myfun(['b','d'], 'foo', 'bar');

How can I call the nested functions whose names are given in input1 array?

Thank You.

1
  • Since you have this[name](); in your code, have a look at the MDN article about this, to get a better idea how it works. Commented Feb 7, 2014 at 6:16

3 Answers 3

3

Edit

As @Felix suggested, there is a much better way (in terms of efficiency) to do this -

var myfun = (function () {
    var util = {
        a: function (){
            console.log('method a');
        },
        b: function (){
            console.log('method b');
        },
        c: function (){
            console.log('method c');
        },
        d: function (){
            console.log('method d');
        }
    };

    return function (input1, input2, input3) {
        for(var i=0; i<input1.length; i++){
            var name = input1[i];
            util[name]();
        }
    };
}());

myfun(['b','d'], 'foo', 'bar');

This way the util object will be constructed only once, where in the previous way the object will be constructed each time myfun is called.

However, please note that in this case, the memory footprint will be larger than the previous one as a reference to util will always be stored in the memory because of closure.


Rather than creating inner functions, try creating an object with methods -

function myfun(input1, input2, input3){
    var util = {
        a: function (){
            console.log('method a');
        },
        b: function (){
            console.log('method b');
        },
        c: function (){
            console.log('method c');
        },
        d: function (){
            console.log('method d');
        }
    };

    for(var i=0; i<input1.length; i++){
        var name = input1[i];
        util[name]();
    }
}

myfun(['b','d'], 'foo', 'bar');
Sign up to request clarification or add additional context in comments.

2 Comments

And depending on the nature of those inner functions, you could use an IIFE to create the object only once and still make the functions inaccessible from the outside. E.g. var myfun = (function() { var util = {}; return function(input1, ...) { ... }; }());.
@FelixKling: Yup, you're absolutely right, I missed that. Thank you for your input, I've edited my answer!
0

replace the name(); with eval(name + '()');

1 Comment

I would not suggest eval for this. It's better to avoid the habit of using eval altogether.
0

EDITED:

You must make the functions members of an object.

var funcFactory = {
    a:function(){
        console.log('func a');
    },
    b:function(){
        console.log('func b');
    },
    c:function(){
        console.log('func c');
    },
    d:function(){
        console.log('func d');
    },
    call:function(input1, input2, input3){     
        for(var i=0; i<input1.length; i++){
           var name = input1[i];
            this[name](); // Doesn't work;
        }
    }
}
funcFactory.call(['b','d'],foo,bar);

3 Comments

Given the OP's code, this would make the functions global, because this will refer to window. You are not making the functions "members" of the outer function.
Yes, I tried this, but as @FelixKling said, it creates this refer to window.
Ahh, right. You would need them to be in an object {a:function(){}...}

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.