我可以将回调函数的所有参数存储到node.js中的数组中吗?

时间:2016-03-24 07:17:05

标签: node.js

我想知道是否存在将回调函数的所有参数存储到数组中的方法?我能否知道回调函数的参数数量?

例如

一些回调函数将接收2个参数错误和行。我可以将它们存储到数组中吗?

c.query('SHOW DATABASES', function(err, rows) {
  if (err)
    throw err;
  console.dir(rows);
});

感谢

2 个答案:

答案 0 :(得分:1)

function foo(){
  var args = Array.prototype.slice.call(arguments);
  console.log(arguments.length);
  console.log(args);
}

foo(1,2,3,'a',true);

答案 1 :(得分:1)

您可以使用arguments对象访问调用函数的参数 功能。 arguments对象是一个类似于object的数组(但不完全是数组)。要获取参数数组,可以使用

var args = Array.prototype.slice.call(arguments);

length属性指定函数预期的参数数量