繁体   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