在 PHP 中有 func_num_args
和 func_get_args
, JavaScript 有类似的东西吗?
最佳答案
对于现代 Javascript 或 Typescript:
class Foo {
reallyCoolMethodISwear(...args) { return args.length; }
}
function reallyCoolFunction(i, ...args) { return args[i]; }
const allHailTheLambda = (...args) => {
return args.constructor == Array;
};
const x = new Foo().reallyCoolMethodISwear(0, 1, 2, 3, 4);
const y = reallyCoolFunction(3, 0, 1, 2, 3, 4, 5, 6);
const z = allHailTheLambda(43110, "world");
console.log(x, y, z); // 5 3 true
对于古老的 Javascript:
使用arguments
.您可以像访问数组一样访问它。使用 arguments.length
作为参数的数量。
关于javascript - 是否可以将函数的所有参数作为该函数内的单个对象获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59528282/