简体   繁体   English

如何执行 JavaScript Function 当我将其名称作为字符串时(使用具有重载参数的参数)

[英]How to execute a JavaScript Function When I have its name as a string (with parameters that have an overload argument)

I am trying to call a certain function based on variables that I pass in (scamId and actorId), and the only issue is that when I try to pass in intent2 as an argument, it does not seem to find what it needs from my repository.我试图根据我传入的变量(scamId 和 actorId)调用某个 function,唯一的问题是当我尝试将 intent2 作为参数传入时,它似乎没有从我的存储库中找到它需要的东西. I got this little function builder script from How to execute a JavaScript function when I have its name as a string and I have used it for some other things and it works great, but I'm not sure how to pass in intent2 and have it create the function correctly.我从如何执行 JavaScript function 中得到了这个小 function 构建器脚本,当我将它的名称作为字符串传递时,我用它来传递它的意图并且我不确定它如何用于其他一些事情,但我不确定它如何工作2,正确创建 function。 It works when the statement is not overloaded like if intent2 = "stuff", but not when I have it in the format below.它在语句没有像 if intent2 = "stuff" 那样重载时起作用,但当我采用以下格式时则不起作用。

For example, return statement would be equivalent to if everything worked correctly -例如,如果一切正常,return 语句将等效于 -

return this.intentS1A1Repository.findOne({where: { intent: intentBase }});

    const info = this

    // how can I format this intent2 so that it works?
    const intent2 = { 
      where: 
      { intent: intentBase }
    }

    const getIntent = "intentS" + scamId + "A" + actorId + "Repository.findOne";
    
    await getIntentFunctionBuilder(getIntent, info, intent2);
    
    async function getIntentFunctionBuilder(functionName: string, context: any, args: any) {
      args = Array.prototype.slice.call(arguments, 2);
      var namespaces = functionName.split(".");
      var func = namespaces.pop();
      for(var i = 0; i < namespaces.length; i++) {
        context = context[namespaces[i]];
      }
    
      return await context[func].apply(context, args);  
    }

Any help would be much appreciated!任何帮助将非常感激!

Okay I figured it out instead of working with that crazy function, its actually much more simple than that.好吧,我想通了,而不是使用那个疯狂的 function,它实际上比这简单得多。

    const intent2 = { 
      where: 
      { intent: intentBase }
    }

    const object = "intentS" + scamId + "A" + actorId + "Repository";
    return await this[object]["findOne"](intent2)

This little code snippet can take in OVERLOADED PARAMETERS as an argument, which is terrific.这个小代码片段可以接受 OVERLOADED PARAMETERS 作为参数,这太棒了。 It allows you to pass in custom built functions, so you don't have to have 20 different functions that perform basically exactly the same task.它允许您传入自定义构建的函数,因此您不必拥有 20 个不同的函数来执行基本完全相同的任务。 The return evaluates to exactly this:返回的计算结果正是这样:

return this.intentS*A*Repository.findOne(intent2);

Hopefully you won't have to go through what I did trying to figure this out.希望您不必通过我试图弄清楚这一点所做的事情来 go 。 Good luck.祝你好运。

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

相关问题 当我将其名称作为字符串时如何执行 JavaScript function - How to execute a JavaScript function when I have its name as a string 当我将其名称作为字符串时执行 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 当我不带参数动态将其名称作为字符串发送时,如何执行javascript函数 - how to execute javascript function when i send its name as string dynamically with out parameters 当我将其名称作为字符串时如何执行Angular控制器方法 - How to execute a Angular controller method 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? 当我有 function 名称时执行 function 的正确方法 - Correct way to execute a function when I have function name 如何在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 如果您在javascript中有其名称的字符串,如何访问该类? - How to access a class if you have a string of its name in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM