Given a command queue looking something like the following . .
var commandQueue = [
function() {thing(100, 0, shiftFunc)},
function() {thing(100, 233, shiftFunc)},
function() {thing(100, 422, shiftFunc)}
];
How can I write a function that takes the commandQueue
as a parameter and returns the sum of the second arguments (index [1]) to the functions in the queue without calling the functions in the queue?
You could use Function#toString
and a regular expression, which search for the value between the first and second comma.
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));
This isn't possible, unfortunately. JavaScript provides no ability to peek inside a function definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With