2

I want to know how to call a function with a string value

In some other questions i found this: window["functionName"](arguments);, and i assume is not working cause my functions are not global, and i don't want them to be global.

I have an example here:

(function ( $ ) {
    "use strict";
    var testing,
    showMe;

    testing = function(){
        alert('Working');
    };

    showMe = function(test){
        window[test](arguments);
    }

    $(document).ready(function(){
        showMe('testing');
    });
})(jQuery);

As you can see, what i want is call the function, testing() using just a String value.

1 Answer 1

2

window is an object. You can place it inside an arbitrary object:

(function ( $ ) {
    "use strict";
    var testing,
    showMe,
    funcs = {};

    funcs.testing = function(){
        alert('Working');
    };

    showMe = function(test){
        funcs[test](arguments);
    }

    $(document).ready(function(){
        showMe('testing');
    });
})(jQuery);
Sign up to request clarification or add additional context in comments.

Comments

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.