简体   繁体   English

如何按名称执行带参数的函数

[英]How to execute function with parameters by its name

I need a way to execute function that has parameters:我需要一种执行具有参数的函数的方法:

func1(param1, param2) 
{...
}

when I get the function name and its parameters as parameters (or string variables) in another function:当我在另一个函数中将函数名称及其参数作为参数(或字符串变量)获取时:

parentFunc()
{
  var funcName="func1";
  var params = {'param1': 'A', 'param2': 'B'}
   //Here I want to execute the funcName (func1) with its params.
}

I've tried creating customEvent, but the param didn't pass:我试过创建customEvent,但参数没有通过:

var event = new CustomEvent("func1", { 
  "param1": 'A', "param2": 'B'
});

this.element.dispatchEvent(event);//The element is the "parent" 
                                  //htmlElement that responsible to the 
                                  //event

*The functions are not in the same file and I'm doing in a Single Page Application (SPA), so doing it as shown below is not good enough: *这些函数不在同一个文件中,我在单页应用程序 (SPA) 中执行,因此按如下所示执行还不够好:

var fn = window[settings.functionName];

Generally - what I'm trying to do is in a custom element - have the option to decide what function is executed (it is in the parent template) when there is event in the custom element通常 - 我想要做的是在自定义元素中 - 当自定义元素中有事件时,可以选择决定执行哪个函数(它在父模板中)

Please consider sending something else to your function than the name of the function to execute.请考虑向您的函数发送除要执行的函数名称之外的其他内容。 You can try for example a mapping between the condition and the function to execute.例如,您可以尝试在条件和要执行的函数之间进行映射。 But if your heart is set on sending the function name or you have no way of not doing it that way, then your friend is the eval function :但是,如果您一心想发送函数名称,或者您无法不这样做,那么您的朋友就是eval函数:

function evaluate(name, params) {
    let toExecute = (name || 'eval') + '(';
    params = (params instanceof Object ? params : {});
    for (let key in params) {
        toExecute += params[key] + ', ';
    }
    if (toExecute[toExecute.length - 2] === ',') {
        toExecute = toExecute.substr(0, toExecute.length - 2);
    }
    toExecute += ');';
    eval(toExecute);
}

function parentFunc() {
    var funcName="func1";
    var params = {'param1': 'A', 'param2': 'B'}

    //Here I want to execute the funcName (func1) with its params.
    evaluate(funcName, params);
}

Please take a moment to read about the dangers of using eval here .请花点时间阅读此处使用eval的危险。

You can passed name function as param to callback:您可以将名称函数作为参数传递给回调:

parentFunc(funcName)
{
        var params = {'param1': 'A', 'param2': 'B'}
        funcName(params);
}

if you want function with params just:如果你想要带有参数的函数:

parentFunc(funcName, param)
{            
        funcName(param);
}

parentFunc(func1, 'A' ) parentFunc(func1, 'A' )

I didn't really understand what you're trying to accomplish by this.我真的不明白你想通过这个来完成什么。 But I'd do something like this:但我会做这样的事情:

var myObject = {
  func1: func1
};

function parentFunc() {
  var funcName="func1";
  var params = {'param1': 'A', 'param2': 'B'};
  myObject[funcName](params.param1,params.param2);     
}

parentFunc();

function func1(param1, param2) {
  console.log(param1);
  console.log(param2);
}

hope it helps.希望能帮助到你。

I managed to do it in Aurelia: This is the custom element with the foucusout.trigger which calls to the focusoutAction in the appropriate timing:我设法在 Aurelia 中做到了:这是带有 foucusout.trigger 的自定义元素,它在适当的时间调用 focusoutAction:

<template>
 <label>
   ${title} - ${fieldName}<input title.bind="title" 
   focusout.trigger="focusoutAction()" focusin.trigger="focusInAction()" 
   class="${cssClass}" />
  </label>
</template>

This is the usage of the custom element in the parent view with the focusout.call attribute:这是带有 focusout.call 属性的父视图中自定义元素的用法:

 <form-input field-name="firstName" title="First Name" 
 place-holder="Enter first name" 
 on-focusout.call="validateInput(nameOfElement)" />

And this is the relevant code in the view model of the custom control:这是自定义控件的视图模型中的相关代码:

@bindable onFocusout;

focusoutAction() {

   var args =
   {
      nameOfElement: this.fieldName
   };
   this.onFocusout(args);
}

暂无
暂无

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

相关问题 当我不带参数动态将其名称作为字符串发送时,如何执行javascript函数 - how to execute javascript function when i send its name as string dynamically with out parameters 如何执行 JavaScript Function 当我将其名称作为字符串时(使用具有重载参数的参数) - How to execute a JavaScript Function When I have its name as a string (with parameters that have an overload argument) 如何执行包含函数名和参数的字符串? - How to execute string containing function name AND parameters? 当其名称作为字符串传递给函数时,如何执行JavaScript函数? - How to execute a JavaScript function when its name is passed as a string in function? 如何在Edge中执行名称为字符串的JavaScript函数 - How to execute a JavaScript function having its name as a string in Edge 当我将其名称作为字符串时如何执行 JavaScript function - How to execute a JavaScript function when I have its name as a string 使用字符串中的名称和参数调用JS函数 - Calling a JS function with its name and parameters in a string 如何声明一个 function 将接受一个 function 参数和参数中没有参数? - How to declare a function that will accept a function with parameters and without parameters in its parameters? 当我将其名称作为字符串时,如何执行私有JavaScript函数 - How to execute a private JavaScript function when I have its name as a string 获取在其参数中包含此函数的函数的名称 - Get the name of a function which has this function in its parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM