给定一个如下所示的命令队列。 .
var commandQueue = [
function() {thing(100, 0, shiftFunc)},
function() {thing(100, 233, shiftFunc)},
function() {thing(100, 422, shiftFunc)}
];
如何编写一个函数,将 commandQueue
作为参数,并将第二个参数(索引 [1])的总和返回给队列中的函数,而不调用队列中的函数?
最佳答案
你可以使用 Function#toString
和一个正则表达式,用于搜索第一个和第二个逗号之间的值。
var commandQueue = [
function() {thing(100, 0, shiftFunc)},
function() {thing(100, 233, shiftFunc)},
function() {thing(100, 422, shiftFunc)}
];
console.log(commandQueue.reduce(function (r, a) {
return r + +((a.toString().match(/,\s*(\d+),/) || [])[1] || 0);
}, 0));
关于javascript - 如何在不调用javascript函数的情况下查询它的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42394242/