如果函数的输入中未定义参数,如何检测传递给函数的参数?

How to detect arguments passed to function if they are not defined in the function's input?

是否可以检测传递给函数的参数,如果它们没有在函数中明确定义?

例如:

function test(){

    //is it possible to see a, b and c from inside this function without defining them in the function's arguments?

};

test(a, b, c);

您可以使用 arguments 对象。

function test(){
  console.log([...arguments]);
};

test('a', 'b', 'c');

在Javascript是上面我们有一个论点

for (let i = 0; i < arguments.length; i++) {
    document.write( arguments[i]);
}

在php中我们有两个函数用于检查PHP函数中的动态参数: 1.func_num_args() 我们可以得到多少个参数 2.func_get_arg(1) 我们可以得到从零开始的参数。

 $args= func_num_args();
 for($i=0;$i> $args;$i++) {
 echo  $i+1. " - argument is: " . func_get_arg($i) . "\n";
 }