bmarti44
bmarti44

Reputation: 1247

Dynamically call method of a class with javascript

I want to dynamically call the method of a custom class much like the below javascript. Except, the javascript below only calls a function that exists in my code. I want to call (dynamically) the function of a class. So I would remove window{value](target, event, self); and use something else that would call the method of a custom created class such as "mycustomclass.anythingcouldbethismethod(target, event, self);" after it had been instantiated of course.

var functions = [
                 'ajaxify_overlay',
                 'ajaxify_overlayCancel',
                 'ajaxify_overlaySubmit',
                 'ajaxify_rollout',
                 'ajaxify_rolloutCancel',
                 'ajaxify_rolloutSubmit',
                 'ajaxify_upload',
                 'ajaxify_contentArea',
                 'ajaxify_itemToggler',
                 'ajaxify_closer',
                 'ajaxify_submit',
                 'ajaxify_inputActivate',
                 'ajaxify_executeAndRefresh',
                 'ajaxify_empty' 
               ];

$(document).bind('ready', function(event) {   

  $('body').live('click', function (event){   

   var target = $(event.target);

   var self = this;  

   $.each(functions, function(index, value){

     if($(target).hasClass(value)) {

       window[value](target, event, self);

     }

          });    

       });

});

Upvotes: 4

Views: 7023

Answers (2)

lincolnk
lincolnk

Reputation: 11238

var myClass = { /* your class definition */ };
var methodName = 'myMethod';
myClass[methodName](p1,p2,...,pN);

Upvotes: 11

user113716
user113716

Reputation: 322452

You mean like this?

function methodCaller( methodName, target, event, self ) {
    mycustomclass[ methodName ](target, event, self);
}

methodCaller( "someMethodName" );

Upvotes: 1

Related Questions