http://jsfiddle.net/trevordixon/zEqKy/
function cartesianCall(func, args) { var combos = allCombos.apply(this, args); for (var i = 0; i < combos.length; i++) { func.apply(null, combos[i]); } } function allCombos(first) { var isArray = toString.call(first) === "[object Array]"; if (!isArray) first = [first];
Then use it as follows:
function printArgs() { console.log('Called with arguments:', arguments); } cartesianCall(printArgs, [ [true, false], undefined, [1, 2], [], 'a string', ['a', 'b', 'c'] ]);
Print
Called with arguments: [true, undefined, 1, undefined, "a string", "a"] Called with arguments: [true, undefined, 1, undefined, "a string", "b"] Called with arguments: [true, undefined, 1, undefined, "a string", "c"] Called with arguments: [true, undefined, 2, undefined, "a string", "a"] Called with arguments: [true, undefined, 2, undefined, "a string", "b"] Called with arguments: [true, undefined, 2, undefined, "a string", "c"] Called with arguments: [false, undefined, 1, undefined, "a string", "a"] Called with arguments: [false, undefined, 1, undefined, "a string", "b"] Called with arguments: [false, undefined, 1, undefined, "a string", "c"] Called with arguments: [false, undefined, 2, undefined, "a string", "a"] Called with arguments: [false, undefined, 2, undefined, "a string", "b"] Called with arguments: [false, undefined, 2, undefined, "a string", "c"]
Note that empty arrays are treated in the same way as undefined .