Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中的参数计数_Javascript_Function_Ecmascript 6 - Fatal编程技术网

Javascript中的参数计数

Javascript中的参数计数,javascript,function,ecmascript-6,Javascript,Function,Ecmascript 6,Mozilla的文档说: console.log((function(...args) {}).length); // 0, rest parameter is not counted console.log((function(a, b = 1, c) {}).length); // 1, only parameters before the first one with // a default value is counted 那么,在这种情况下,我如何计算参数

Mozilla的文档说:

  console.log((function(...args) {}).length); 
  // 0, rest parameter is not counted

  console.log((function(a, b = 1, c) {}).length);
  // 1, only parameters before the first one with 
  // a default value is counted
那么,在这种情况下,我如何计算参数呢


这里有两个独立的事情,定义了参数,实际传递了参数

对于已定义的函数,您可以使用fn.length访问函数定义中定义的参数数量:

另外,在一个函数中,您可以看到对于使用arguments.length的函数的给定调用,有多少个参数实际传递给该函数。假设您编写了一个函数,以接受可选回调作为最后一个参数:

function talk(greeting, delay, callback) {
    console.log(arguments.length);     // shows how many arguments were actually passed
}

talk("hello", 200);    // will cause the function to show 2 arguments are passed
talk("hello", 200, function() {    // will show 3 arguments are passed
     console.log("talk is done now");
});                                 

这里有两个独立的事情,定义了参数,实际传递了参数

对于已定义的函数,您可以使用fn.length访问函数定义中定义的参数数量:

另外,在一个函数中,您可以看到对于使用arguments.length的函数的给定调用,有多少个参数实际传递给该函数。假设您编写了一个函数,以接受可选回调作为最后一个参数:

function talk(greeting, delay, callback) {
    console.log(arguments.length);     // shows how many arguments were actually passed
}

talk("hello", 200);    // will cause the function to show 2 arguments are passed
talk("hello", 200, function() {    // will show 3 arguments are passed
     console.log("talk is done now");
});                                 


计算函数外部的参数数或计算传递给函数的参数数?为什么需要计算参数数?为什么你的标题上写的是论点的数量?这些是不同的东西。你为什么会关心参数的数量呢?@Bergi我正在学习JS,探索一切都没有错,将来我可能需要它!:仅供参考,计算参数的预期长度和传递的参数长度之间存在差异…计算函数外部参数的数量还是计算传递给函数的参数的数量?为什么需要计算参数?为什么你的标题上写的是论点的数量?这些是不同的东西。你为什么会关心参数的数量呢?@Bergi我正在学习JS,探索一切都没有错,将来我可能需要它!:仅供参考,计算参数的预期长度和传递的参数长度之间存在差异……对于第二种解决方案,不能只计算调用时传入的参数吗?@Pineda-什么是计数?为什么要在arguments.length直接给出数字时计算它们?我想您可以迭代arguments对象以查看它有多少属性,但我看不到arguments.length已经包含所需计数的点。我指的是OP试图从函数外部实现这一点的方式。我试图建议进行一次编辑,以显示函数内部而非外部参数的有用性。“在定义参数和实际传递参数的情况下,这里有两种不同的情况”-这就是为什么我们对它们有不同的术语“实际传递的参数”是参数,“已定义的参数”是参数。@estus-我更新了答案以使用正确的术语。对于第二个解决方案,不能只计算调用时传递的参数吗?@Pineda-计算是什么意思?为什么要在arguments.length直接给出数字时计算它们?我想您可以迭代arguments对象以查看它有多少属性,但我看不到arguments.length已经包含所需计数的点。我指的是OP试图从函数外部实现这一点的方式。我试图建议进行一次编辑,以显示函数内部而非外部参数的有用性。“在定义参数和实际传递参数的情况下,这里有两种不同的情况”-这就是为什么我们对它们有不同的术语“实际传递的参数”是参数,“定义的参数”是参数。@estus-我更新了我的答案以使用正确的术语。