How to create a closure and pass in variable length argument list?

Posted by Jian Lin on Stack Overflow See other posts from Stack Overflow or by Jian Lin
Published on 2010-05-06T04:45:10Z Indexed on 2010/05/06 4:48 UTC
Read the original article Hit count: 289

We can create a closure p by capturing the arguments in the scope in the following code:

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

but the arguments are passed like an array to console.log, instead of passed one by one as in

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

Is there a way to expand the arguments and pass to console.log like the way above?

Note that

p = console.log;

works well in Firefox and IE 8 but not on Chrome.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about closure