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,753
  • 72
  • 57
  • 129
VelNaga
  • 3,593
  • 6
  • 48
  • 82
  • 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]()`. – Roamer-1888 May 19 '15 at 03: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? – Julian Oct 09 '21 at 12:18

1 Answers1

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>
guest271314
  • 1
  • 15
  • 104
  • 177
  • 1
    …or much better an own, local object, instead of polluting the global jQuery namespace. – Bergi May 19 '15 at 04:33
  • Well there was nothing wrong with exporting `preRender` to `$`, only all those dynamically-named function should stay inside the IIFE. – Bergi May 19 '15 at 04:52
  • @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 ? – guest271314 May 19 '15 at 04:58
  • @Bergi What could be improved at above solutions ? – guest271314 May 19 '15 at 05:03
  • 1
    Just what I said: Merge them so that you use `localObject` for `launchmenu` but `$` for `preRender `. – Bergi May 19 '15 at 05:05