Kokizzu Kokizzu - 11 days ago 6
Javascript Question

javascript calling console.log function with variable length

i want to call console.log function with variable length argument

function debug_anything() {
var x = arguments;
var p = 'DEBUG from ' + (new Error).stack.split("\n")[2];
switch(x.length) {
case 0: console.log(p); break;
case 1: console.log(p,x[0]); break;
case 2: console.log(p,x[0],x[1]); break;
case 3: console.log(p,x[0],x[1],x[2]); break;
case 4: console.log(p,x[0],x[1],x[2],x[3]); break;
// so on..
}
}


is there any (shorter) other way,
note that i do not want this solution
(since other methods from the x object (Argument or array) would be outputted.

console.log(p,x);

Answer
function debug_anything() {
  var x = arguments;
  var p = 'DEBUG from ' + (new Error).stack.split("\n")[2];
    console.log.apply(console, [p].concat(Array.prototype.slice.call(x)));
}