簡體   English   中英

如何在Node.js中查找進入構造函數的參數數量

[英]How to find number of arguments that come into a constructor in node.js

我在node.js中有一個構造函數,如下所示。

function Tree() {
  //Redirect to other functions depends upon argument count.
}

我創建了像

var theTree = new Tree('Redwood');
var theTree = new Tree('Redwood',5);
var theTree = new Tree('Redwood',10,"USA");

我的要求是,我想根據傳入構造函數的參數數量重定向到不同的函數。 如何找到參數個數?

您只需使用arguments.length變量。

有關更多信息,您可以閱讀有關參數對象的信息: https : //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/arguments

每個函數中都有一個自變量變量。

function Tree() {
  console.log(arguments)
}

有一個局部變量參數,其中包含所有傳遞給函數的值。

arguments.length 

這將給計數。

暫無
暫無

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

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