This may not work everywhere, but it's a neat trick to succinctly replicate {...}
in JavaScript.
function varg(f, a) {
f = f || varg.caller || arguments.caller;
a = a || f.arguments;
return Array.prototype.slice.call(a, f.length);
}
function f(a) {
var b;
// varg() could be used in one of the following ways:
b = varg(); // Non-standard: uses caller.
b = varg(f); // Not strict compliant: uses a function's arguments property.
b = varg(f, arguments); // Should be safe to use everywhere.
return b;
}
console.log(f(1));
console.log(f(1, 2));
console.log(f(1, 2, 3));
What's going on here is that within varg()
, the calling function is inferred from caller
and its arguments are determined by checking its arguments
property. The argument pseudo-array is then sliced into a real array, starting from f.length
, which represents the arity, or how many arguments is expected of the calling function, thus avoiding the need for any hard-coded magic numbers.
However, if the problem you're trying to solve is just redirecting a function's arguments into another's, then T.J. Crowder's answer is more appropriate.
function FuncCallHack(Func) {
return Func.apply(this, [].slice.call(arguments, 1));
}