如何调用一个函数,其名称是通过在node.js中组合一些变量来动态创建的

How can i call a function, whose name is dynamically created by combining some variables in node.js?

本文关键字:组合 node 合一 js 变量 创建 动态 调用 何调用 一个 函数      更新时间:2023-09-26

>我在 Node 中有一个构造函数.js

    function Response() {
                var numberOfArguments = arguments.length;
                var getArguments = arguments;             
                var newConstructor = arguments.callee.name + i; // ie, (Constructor Name + numberOfArguments)
                /* Here i want to call the constructor with the value in newConstructor and pass the arguments in getArguments*/
               // ie, call function like Response0();
               //                        Response1(getArguments[0]);
    }
    function Response0() {
         //Constructor Body for Response0()
    }
    function Response1(sid) {
          //Constructor Body for Response0()
    }

现在我的问题是我如何根据进入 Response 构造函数的参数数量调用这些函数 Response0() 或 Response1()。

我没有

尝试过这个,但也许这是一种方法:

Response0() {
     //Constructor Body for Response0()
}
Response1(sid) {
      //Constructor Body for Response0()
      //NOTE: sid will be an array of length 1
}
//here we create kind of a lookup table that binds constructors to a specific number of arguments
var dictionary = {
    0 : Response0,
    1 : Response1
};
Response() {
           var args = Array.prototype.slice.call(arguments),
               numberOfArguments = args.length;
           if (dictionary.hasOwnProperty(numberOfArguments) {
               //here we call the constructor and give an array of the current args to work with
               new dictionary[numberOfArguments](args);
           }
}