簡體   English   中英

我如何調用一個函數,該函數的名稱是通過結合node.js中的一些變量來動態創建的?

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

我在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()
    }

現在我的問題是我該如何調用這些函數Response0()或Response1(),具體取決於進入Response構造函數的參數數量。

我沒有嘗試過,但是也許這是一種方法:

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);
           }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM