Appunni Appunni - 7 months ago 6
Javascript Question

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 ?

Answer

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

Comments