简体   繁体   English

使用javascript动态调用类的方法

[英]Dynamically call method of a class with javascript

I want to dynamically call the method of a custom class much like the below javascript. 我想动态调用自定义类的方法,就像下面的javascript一样。 Except, the javascript below only calls a function that exists in my code. 除此之外,下面的javascript只调用我的代码中存在的函数。 I want to call (dynamically) the function of a class. 我想调用(动态)类的功能。 So I would remove window{value](target, event, self); 所以我会删除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);" 并使用其他可以调用自定义创建类的方法的东西,例如“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);

     }

          });    

       });

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

You mean like this? 你的意思是这样的?

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

methodCaller( "someMethodName" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM