Possible Duplicate:
How to create a function and pass in variable length argument list?
I want to call the console.log with variable argument list
console.log("a","b")
console.log("a","b","c")
but i get the arguments from an array:
var arr = ["a","b","c"];
and i want to pass as single variables not as a complete array.
so console.log(arr) is not what i am looking for,console.log(arr[0],arr[1],arr[2]) is also bad because i dont know the array length ofc.
How can i do that?
the console.log is only an example, i would use this in different problems
UPDATE
How to create a function and pass in variable length argument list? is not good. because according to the answer
function dump(a,b) {
console.log("a:"+a,"b:"+b);
}
var asd = [1,2,3]
dump.call(this,asd)
should give an output: a:1,b:2 instead of a:[1,2,3] b:undefined
UPDATE:
maybe my question was not clear enough, sorry.
The console.log is only a example of variable argument invoking
i want to use the same method for different problems
look at this example:
function Sum() {
var temp = 0;
for(var i=0;i<arguments.length;++i) {
temp+= arguments[i];
}
return temp;
}
and i want to call with different arguments which are in an array.
var test1 = [1,2,3];
var test2 = [4,5,6];
var a = Sum.call(this,test1) //this gives an output "01,2,3"
var b;
for(var i=0;i<test2.length;++i) {
b = Sum(test2[i])
} //this is also bad because it only returns 6 at the last invoke.
Function.calleven mentioned. Do you mean to ask 'why doesdump.call(this,asd)result in an output of "a:[1,2,3] b:undefined"?'