简体   繁体   English

如何在Node.js中查找进入构造函数的参数数量

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

I have a constructor in node.js as follows. 我在node.js中有一个构造函数,如下所示。

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

And i created objects like 我创建了像

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

My requirement is that i want to redirect to different functions depending upon the number of arguments that came to constructor. 我的要求是,我想根据传入构造函数的参数数量重定向到不同的函数。 How can I find the number of arguments ? 如何找到参数个数?

You simply use the arguments.length variable. 您只需使用arguments.length变量。

For more info you can read about the arguments object: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/arguments 有关更多信息,您可以阅读有关参数对象的信息: https : //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/arguments

There is an arguments variable in every function. 每个函数中都有一个自变量变量。

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

There is a local variable arguments that contains all the values that are passed to a function. 有一个局部变量参数,其中包含所有传递给函数的值。

arguments.length 

this will give the count. 这将给计数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM