I've seen the code for this function in John Resig's book 'Secrets of JavaScript Ninja' yesterday.
I considered it useful at once because I've disliked repeatedly typing console.log
for some time.
Here's my implementation of a helper function to reduce typing:
// -- Function definition -------------
// Helper function for to simplify the
// usage of console.log().
// -- Parameter -----------------------
// Pass as many parameter as you like.
// Separate them with commas.
function l() {
console.log.apply(console, arguments);
}
Usage examples:
var car = new Car(180, 'Fiat');
var someArbitraryNumber = 248;
l('My car', car, ' - ', someArbitraryNumber);
l(123, 'abc', false, 248, 'A', ' > --- < ')
Output:
My car Car { color="red", maxFuel=40, speed="180 km"} - 248 123 abc false 248 A > --- <
What's great about it: instead of typing console.log(...)
, you can type l(...);
much quicker! This simple helper behaves exactly like console.log
. For example, the parameters can be any mixture of types, and objects printed on your browser console can be expanded to see their content, they aren't converted to string representations.
If there are any ideas to improve / enhance the function I would be pleased to get that feedback.
2 Answers 2
As I commented on the question, this has no difference from running console.log
without your function.
As saying on the documentation of Function.prototype.apply()
:
The
apply()
method calls a function with a giventhis
value andarguments
provided as an array (or an array-like object).
The arguments
object is an "array-like object"
. What is an array-like object? Quoting the documentation on "Working with array-like objects"
, at MDN:
Some JavaScript objects, such as the NodeList returned by document.getElementsByTagName() or the arguments object made available within the body of a function, look and behave like arrays on the surface but do not share all of their methods. The arguments object provides a length attribute but does not implement the forEach() method, for example.
Basically: you are running console.log()
straight without ANY modification what-so-ever.
Also, your function fails on IE8, since it only makes the console
object available when you open the developer tools (F12). Quoting a great answer, from Mister Lucky on StackOverflow (stylized by me):
console.log
is only available after you have opened the Developer Tools (F12 to toggle it open and closed).
This means that your code will break with a ReferenceError: console is undefined
on IE8, just like running console.log()
without your function.
Concluding this review: Your code is pretty much useless and doesn't do anything.
That is more an example code on how to use the method .apply()
, it's usefullness and how to use the arguments
object.
It isn't serious code!
If you want some serious code, you could use the following alternative:
function log() {
if ('console' in self && 'log' in console) {
console.log.apply(console, arguments);
}
}
It is loosely based on your code and loosely based on the code presented on Mister Lucky's answer.
This way, you have an actual reason to call console.log()
using the .apply()
method and you are actually fixing a problem that could bite you in the future.
l
is a terrible name for anything. Depending on the font used, it may be indistinguishable from the number one. It's generally recommended to avoid using the letter "l" for anything. (In some languages this recommendation is part of the official style guide.)
log
would be better, still short, and with a more obvious meaning than just "l".
console.log
. It isn't a real helper since it isn't helping on anything. I see no difference between your code andconsole.log(...)
. \$\endgroup\$