简体   繁体   English

当我不带参数动态将其名称作为字符串发送时,如何执行javascript函数

[英]how to execute javascript function when i send its name as string dynamically with out parameters

I want execute javascript function which the name is coming as a string dynamically. 我想执行名称动态作为字符串来的javascript函数。 ansd i dont need to pass any parameters while executing the function. Ansd我在执行功能时不需要传递任何参数。

Please can any one guide me how to achieve this? 请任何人指导我如何实现这一目标?

Regards, Kamesh 问候,卡梅什

one simple way 一种简单的方法

eval("SomeFunction()");

or 要么

var funcName = "SomeFunction";
var func == window[funcName];
func();

危险,但您可以使用eval(method_name+"()")

are you talking about ´eval()´?? 您在说“ eval()”吗?

var foo = "alert('bar')";
eval(foo);

Hope this helps; 希望这可以帮助;

function a() { 
   console.log('yeah!'); 
}

var funcName = 'a'; // get function name

this[funcName]();

如果该函数是全局函数,则应在浏览器中执行window[funcName]()

Using eval is the worst way imaginable. 使用eval是可想象的最糟糕的方法。 Avoid that at all costs. 不惜一切代价避免这种情况。

You can use window[functionname]() like this: 您可以像这样使用window[functionname]()

function myfunction() {
    alert('test');
}

var functionname = 'myfunction';
window[functionname]();

This way you can optionally add arguments as well 这样,您还可以选择添加参数

Perhaps a safer way is to do something like this (pseudo code only here): 也许更安全的方法是执行以下操作(仅在此处使用伪代码):

function executer(functionName)
{
  if (functionName === "blammo")
  {
    blammo();
  }
  else if (functionName === "kapow")
  {
    kapow();
  }
  else // unrecognized function name
  {
    // do something.
  }

You might use a switch statement for this (it seems like a better construct): 您可以为此使用switch语句(似乎是一种更好的构造):

switch (functionName)
{
  case "blammo":
    blammo();
    break;

  case "kapow":
    kapow();
    break;

  default:
    // unrecognized function name.
}

You can optimize this by creating an array of function names, searching the array for the desired function name, then executing the value in the array. 您可以通过创建一个函数名称数组,在该数组中搜索所需的函数名称,然后执行该数组中的值来优化此功能。

暂无
暂无

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

相关问题 如何执行 JavaScript Function 当我将其名称作为字符串时(使用具有重载参数的参数) - How to execute a JavaScript Function When I have its name as a string (with parameters that have an overload argument) 当我将其名称作为字符串时如何执行 JavaScript function - How to execute a JavaScript function when I have its name as a string 当其名称作为字符串传递给函数时,如何执行JavaScript函数? - How to execute a JavaScript function when its name is passed as a string in function? 当我将其名称作为字符串时执行 JavaScript 函数 - 不工作 - Execute a JavaScript function when I have its name as a string - Not working 当我将其名称作为字符串时,如何执行私有JavaScript函数 - How to execute a private JavaScript function when I have its name as a string 如何在Edge中执行名称为字符串的JavaScript函数 - How to execute a JavaScript function having its name as a string in Edge 如何按名称执行带参数的函数 - How to execute function with parameters by its name 如何从HTML元素执行javascript函数并发送该元素名称,而无需专门使用其名称? - How do I execute a javascript function from an HTML element and send that elements name without use its name specifically? 如何执行包含函数名和参数的字符串? - How to execute string containing function name AND parameters? 当我将其名称作为字符串时如何执行Angular控制器方法 - How to execute a Angular controller method when I have its name as a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM