Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

     }

          });    

       });

});
like image 573
bmarti44 Avatar asked Oct 29 '25 14:10

bmarti44


2 Answers

var myClass = { /* your class definition */ };
var methodName = 'myMethod';
myClass[methodName](p1,p2,...,pN);
like image 142
lincolnk Avatar answered Oct 31 '25 04:10

lincolnk


You mean like this?

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

methodCaller( "someMethodName" );
like image 40
user113716 Avatar answered Oct 31 '25 04:10

user113716



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!