Let's take a look at this code:
var mainFunction = function() {
altFunction.apply(null, arguments);
}
The arguments that are passed to mainFunction are dynamic -- they can be 4 or 10, doesn't matter. However, I have to pass them through to altFunction AND I have to add an EXTRA argument to the argument list.
I have tried this:
var mainFunction = function() {
var mainArguments = arguments;
mainArguments[mainArguments.length] = 'extra data'; // not +1 since length returns "human" count.
altFunction.apply(null, mainArguments);
}
But that does not seem to work. How can I do this?
not +1 since length returns "human" count?.length. When you usearr[arr.length] = "whatever", it's the same behavior as using.push(), and just adds the item to the end of the array. Your problem is thatargumentsis not an array, so that's why you need to use one of the many similar solutions that were provided.arguments.length++before calling apply.