在JavaScript中查找函数参数的数量 [英] Finding the number of function parameters in JavaScript

查看:66
本文介绍了在JavaScript中查找函数参数的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

获得功能的支持

说我有:

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

我想写一个名为numberOfParameters的函数,它返回一个函数通常接受的参数个数。所以......

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


推荐答案

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

这篇关于在JavaScript中查找函数参数的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆