Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript determine number of arguments a function takes

Tags:

javascript

Say I have some functions stored in an array of objects, each takes a variable amount of arguments:

var ops = [
   {
      id: "plus",
      op: function(a, b) {
         return a + b;
      }
   },
   {
      id: "inverse",
      op: function(a) {
         return -a;
      }
   }
];

How would I determine the number of arguments the function takes from outside the function (Without specifying it as an additional property) e.g.

for (var i = 0; i < ops.length; i++) {
   var op = ops[i].op;
   var args = ? // Determine number of arguments op takes here
}

Is this even possible? If so how?

like image 721
George Reith Avatar asked Aug 28 '26 18:08

George Reith


1 Answers

Yes, it's possible. Use function.length

like image 153
Kenan Banks Avatar answered Aug 30 '26 09:08

Kenan Banks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!