我怎样才能让变量的值是一个函数名? [英] how can i make a value from variable be a function name?

查看:160
本文介绍了我怎样才能让变量的值是一个函数名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个练习程序,从json创建一个动态菜单。然后,每当我点击那个动态菜单,一个标签就会出现一个标签名称,取决于你点击的标签名称是什么。



现在我想要的是什么,我仍然搞清楚,是每当我点击菜单时,它会调用一个与我点击的菜单名称相同的函数,但我不知道该怎么做。我的js函数是一个单独的文件。这里是代码:

  .... 
$('。menu')。click(function(){
dyna_tabs.add('Tab'+ $(this).attr(rel),//这是用于< a href = - >
$(this).attr (rel),//如果是标题
$(this).html()); //为tab_content
fname = $(this).html(); // this是我的变量名称,我打算用作函数名称
alert('这将是我的函数名称'+ fname);
fname();
});
....

你可以找到其余的动态标签代码这里



so ...我输入 fname(); c $ c>其中fname保存所谓的我的函数名称的值。但每次我运行我的程序时,错误消息显示fname不是函数。任何人都可以知道如何做到这一点吗?请.... ....

还有一件事,是否有人知道如何把我的动态标签中的一些HTML代码。或者里面有一个.html页面?感谢您的阅读

解决方案

您可以使用另一个函数来调用必要的函数。像:

  loader(fname); 

函数加载器{
switch(s){
case'xy':xy();打破;
...
}
}

您的其他选项可能会(

  var myFunctions = {
'xy':function(){
//在这里做点什么
},
...
};

然后您可以轻松调用它们:

  myFunctions [FNAME]; 

这些都不是完整的解决方案,只是您可以遵循的一些准则。与eval相比,其优势在于,通过这些解决方案,不会发生您不打算运行的功能可能会运行的情况。只有预定义的可以被调用。


i created an exercise program that create a dynamic menu from json. and then, everytime i clicked that dynamic menu, a tab will appear having a tab name depending on what is the name of the tab you are clicking.

what i want now, that i'm still figuring out, is that everytime i clicked the menu, it will call a function which have the same name of the menu i clicked., but i just dont know how to do it. my js functions is a separate file. here's the code:

    ....
    $('.menu').click(function () {
       dyna_tabs.add('Tab' + $(this).attr("rel"),  //this is for the <a href= "-">
       $(this).attr("rel"),                         //this if for the title
       $(this).html());                             // for the tab_content
       fname = $(this).html();                     // this is my variable name which i plan to use as function name
       alert('this would be my function name ' + fname);
       fname();
    }); 
    ....

you can find the rest of the dynamic tab code here. i just did some edit that would create a menu then tab from the menu.

so... i type fname(); where fname holds the value of the supposedly my function name. but everytime i run my program, an error message shows "fname is not a function". can anybody here knows how to do this right? please....

and one more thing, is anybody knows how put some html codes inside my dynamic tab.? or a .html page inside it? thank you for reading

解决方案

You can use another function that will call the necessary function. Like:

loader(fname);

function loader(s) {
  switch(s) {
    case 'xy': xy(); break;
    ...
  }
}

Your other option might be to store your functions in an object:

var myFunctions={
  'xy' : function () {
    //do something here
  },
  ...
};

and then you can call them easily like:

myFunctions[fname];

These are not complete solutions, just some guidelines that you could follow. The advantage over eval is that with these solutions it cannot happen that a function you have not intended to run could run. Only the predefined ones can be called.

这篇关于我怎样才能让变量的值是一个函数名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆