Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do console.log() polyfills not use Function.apply()?

I've been looking at some of the popular console.log() wrappers/polyfills:

  • Paul Irish's
  • Ben Alman's
  • Craig Patik's

I notice that all of them accept multiple arguments, but they all do something like this:

console.log(arguments);

Which results in output like this (in Chrome):

<code>console.log(['foo', 'bar', $('body')])</code>

Whereas, at least in a modern browser like Chrome or Firefox, console.log() also accepts multiple arguments, so that this would produce (IMHO) superior output:

console.log.apply(console, arguments)

Which results in output like this (in Chrome):

<code>console.log.apply(console, ['foo', 'bar', $('body')])</code>

Is there any particular reason why I should avoid using console.log.apply() with multiple arguments? Or this this just a matter of taste or saving bytes?

like image 681
David Eyk Avatar asked Jul 20 '12 17:07

David Eyk


1 Answers

I would personally suggest that you only use .apply() when you have to: .apply() is the only way to pass an array as the arguments of a function. If you don't need to pass an array, then just use console.log(). It is less verbose and it is a direct invocation.

like image 181
MateyY Avatar answered Nov 11 '22 16:11

MateyY