简体   繁体   中英

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

I have a constructor in node.js as follows.

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.

For more info you can read about the arguments object: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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