0

I have this module:

define(function() {
   clickHandlerA = function() { ... }
   clickHandlerB = function() { ... }

   return {
     handle : function(param) {
        doSomething(param);
        var handler = 'clickHandler' + param;
     }
   }
}

Now, I need to somehow call the appropriate click handler. I tried

if (typeof handler  == 'function') {
   handler.call();
}

I also tried

if (handler in this) {
   handler();
}

but none work. Any suggestions?

Banana
  • 4,010
  • 9
  • 33
  • 49

1 Answers1

1

What I understand you need is this :

define(function() {
   var handlers = {
      A: function() { ... },
      B: function() { ... }
   };

   return {
     handle : function(param) {
        doSomething(param);
        return handlers[param];
     }
   }
}

So you may do this from outside

 yourModule.handle('A')();
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758