How do I create a function and pass it to a variable length argument list?

We can create a function p

with the following code:

var p = function() { };
if (typeof(console) != 'undefined' && console.log) {
    p = function() { console.log(arguments); };
}

      

but the arguments are passed as an array in console.log

and not passed one by one as in

console.log(arguments[0], arguments[1], arguments[2], ... 

      

Is there a way to expand the arguments and go to console.log as shown above?

Note that if the original code was

var p = function() { };
if (typeof(console) != 'undefined' && console.log) {
    p = console.log;
}

      

then it works well on Firefox and IE 8 but not on Chrome.

+2


a source to share


1 answer


You can use Function.apply () :



console.log.apply(console, arguments);

      

+8


a source







All Articles