gpt4 book ai didi

javascript - 获取函数的元数

转载 作者:IT王子 更新时间:2023-10-29 02:58:28 26 4
gpt4 key购买 nike

在 Javascript 中,如何确定为函数定义的形式参数的数量?

请注意,这不是调用函数时的arguments 参数,而是定义函数时使用的命名参数的数量。

function zero() {
// Should return 0
}

function one(x) {
// Should return 1
}

function two(x, y) {
// Should return 2
}

最佳答案

> zero.length
0
> one.length
1
> two.length
2

Source

一个函数可以像这样确定它自己的元数(长度):

// For IE, and ES5 strict mode (named function)
function foo(x, y, z) {
return foo.length; // Will return 3
}

// Otherwise
function bar(x, y) {
return arguments.callee.length; // Will return 2
}

关于javascript - 获取函数的元数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4848149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com