title: dynamically call local function in javascript
body: <p>there are plenty of similar questions out there about calling functions by name dynamically. However, I can't find a solution to my specific problem where I have local functions inside a closure without exposing the functions to the public interface of my object.</p>
  
  <p>Lets see some code (this is a fictional example)...</p>
  
  <pre><code>(function(window,$) {
  
    MyObject = (function($) {
      var obj = {};
      obj.publicMethod = function(number,otherarg) {
        this['privateMethod'+number].apply(this,[otherarg]);
      };
  
      var privateMethod1 = function(arg) {
        //do something with arg
      };
  
      var privateMethod2 = function(arg) {
        //do something else with arg
      };
  
      return obj;
    })($);
  
    window.MyObject = MyObject;
  })(window,jQuery);
  </code></pre>
  
  <p>This doesn't work because "this" is MyObject and the local functions are not exposed.
  Also I'd like to be able to check if the function exists before trying to call it.
  eg.</p>
  
  <pre><code>var func_name = 'privateMethod'+number;
  if($.isFunction(this[func_name])) {
    this[func_name].apply(this,[otherarg]);
  }
  </code></pre>
  
  <p>I'm not really sure how to proceed, short of exposing my private functions to the public interface, it all works then.</p>
  
  <pre><code>obj.privateMethod1 = function(arg) {
    //do something with arg
  };
  
  obj.privateMethod2 = function(arg) {
    //do something else with arg
  };
  </code></pre>
  
  <p>I'm running out of ideas. Your help and advise is greatly appreciated.</p>