I have an array with random count of elements, for example:
var array = [1, 2];
And I want to execute some function with the next parameters:
functionName(SOME_CONST, array[0], array[1])
What is the best way to do this, considering that array could have a different number of parameters?
Have a look at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply for details on how to use the Function.prototype.apply method.
So long as you want to use all of the items in your array as arguments in the function that you are calling, you can use:
which will result in calling some_method with a being the ‘SOME_CONST’ and b and c being the first two elements in the array. If you had more elements in the array, they would come next in the arguments list in the some_method function.