1

I have a jQuery function which accepts few arguments based on the argument I should call another jQuery function.

example:

arg0 = "Launch"
 arg1 = "menu"

example:

(function($)
 {
 preRender: function(arg0,arg1)
 {
 var nextFuncName = arg0+arg1; //launchmenu
 nextFuncName() // hope to call the function name framed from the number of arguments
 }
})(jQuery);
 (function($)
 {
 launchmenu: function()
 {
 console.log("Launchmenu called");
 }
})(jQuery);

I referred the following link calling a jQuery function named in a variable but I don't want to use eval() or window object.

Is it possible to use $.proxy or any other method to achieve this functionality, also is it possible to achieve this using underscore.js?

Any help will be appreciated.

Brian Tompsett - 汤莱恩
5,92772 gold badges64 silver badges135 bronze badges
asked May 19, 2015 at 3:47
2
  • 4
    You can only do this if the full set of possible functions is defined as properties of some object, myObject. Then you can write myObject[arg0 + arg1](). Commented May 19, 2015 at 3:51
  • Why is each of your functions defined in a separate immediately-invoked function expression (IIFE) that takes jQuery as an argument? And by a "jQuery function" do you mean such a function defined inside an IIFE, or do you actually mean a method of the jQuery library? Commented Oct 9, 2021 at 12:18

1 Answer 1

2

Try utilizing jQuery object

...or much better an own, local object, instead of polluting the global jQuery namespace.


Merge them so that you use localObject for launchmenu but $ for preRender


(function($) {
 var localObject = {};
 function launchmenu() {
 console.log("Launchmenu called")
 }
 function preRender(arg0, arg1) {
 var nextFuncName = arg0 + arg1;
 if (nextFuncName in localObject) {
 localObject[nextFuncName]()
 } else {
 console.log(typeof nextFuncName)
 }
 }
 localObject.launchmenu = launchmenu;
 $.preRender = preRender;
 
}(jQuery));
$(function() {
 $.preRender("launch", "menu");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

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

5 Comments

...or much better an own, local object, instead of polluting the global jQuery namespace.
Well there was nothing wrong with exporting preRender to $, only all those dynamically-named function should stay inside the IIFE.
@Bergi When defining outside IIFE , as named function , attempt is to preserve ability to utilize outside of "local object" , or jQuery - though would change the $ within if condition to this , so call, apply could be utilized ; without redefining $ before, when called. Not certain interpreting "only all those dynamically-named function should stay inside the IIFE." portion correctly ? Can describe details ?
@Bergi What could be improved at above solutions ?
Just what I said: Merge them so that you use localObject for launchmenu but $ for preRender .

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.