Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the number of function parameters in JavaScript [duplicate]

Tags:

javascript

Possible Duplicate:
Get a function's arity

Say I have:

function a(x) {};
function b(x,y) {};

I want to write a function called numberOfParameters that returns the number of parameters that a function normally accepts. So...

numberOfParameters(a) // returns 1
numberOfParameters(b) // returns 2
like image 570
Steve Avatar asked Jan 20 '23 13:01

Steve


1 Answers

function f(x) { }
function g(x, y) { }

function param(f) { return f.length; }

param(f); // 1
param(g); // 2

Disclaimer: This should only be used for debugging and auto documentation. Relying on the number of parameters that a function has in it's definition in actual code is a smell.

.length

like image 142
Raynos Avatar answered Jan 22 '23 02:01

Raynos