Possible Duplicate:
alias to chrome console.log
This is really silly, but I can't abbreviate console.log (I'm using Chrome). Here's my naive attempt:
var log = console.log;
log("Bingo"); // Uncaught TypeError: Illegal invocation
Should I be using apply()? But then I'd have to pass along the arguments, right?
log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console. log(A);
Right click on the object in console and click Store as a global variable. the output will be something like temp1. type in console copy(temp1) paste to your favorite text editor.
JavaScript Math log() The JavaScript Math. log() function returns the natural logarithm of a number. It returns the natural logarithm (base e) of a number.
This fails because references to Javascript methods do not include a reference to the object itself. A simple and correct way to assign the method console.log
to a variable, and have the call apply to console
, is to use the bind
method on the log
method, passing console
as the argument:
var log = console.log.bind(console);
There is a hidden this
argument to every method, and because of arguably bad language design, it's not closured when you get a reference to a method. The bind
method's purpose is to preassign arguments to functions, and return a function that accepts the rest of the arguments the function was expecting. The first argument to bind
should always be the this
argument, but you can actually assign any number of arguments using it.
Using bind
has the notable advantage that you don't lose the method's ability to accept more arguments. For instance, console.log
can actually accept an arbitrary number of arguments, and they will all be concatenated on a single log line.
Here is an example of using bind
to preassign more arguments to console.log
:
var debugLog = console.log.bind(console, "DEBUG:");
Invoking debugLog
will prefix the log message with DEBUG:
.
The simple way will be to wrap it in a function:
var log = function (l) { console.log(l); }
But make note that console.log can take an unlimited number of arguments so the appropriate way will be do this:
var l = function () { console.log.apply(console, arguments); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With