Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node vs Chrome, assigning console.log to a variable?

When I assign console.log to a variable in node.js it works fine,

var l = console.log
l(1) # outputs 1

However, if I do the same thing in Chromium 30's dev tools,

var l = console.log
l(1) # TypeError: Illegal invocation

How come it doesn't work in Chromium's dev tools? Why am I getting,

TypeError: Illegal invocation

like image 278
NO WAR WITH RUSSIA Avatar asked Nov 10 '13 07:11

NO WAR WITH RUSSIA


People also ask

How do you assign a console log to a variable?

If you want to do this to an object that has been already logged (one time thing), chrome console offers a good solution. Hover over the printed object in the console, right click, then click on "Store as Global Variable". Chrome will assign it to a temporary var name for you which you can use in the console.

Can you console log a variable?

The console.log() is a function in JavaScript that 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(" ");

Does console log work in node?

log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.

Does console log slow down node?

As stderr is always written to synchronously, therefore in node. js any use of console. error, or other functions that write to stderr, will block your process until the output has all been written. The method is useful for error messages, but excessive use could slow down your process.


2 Answers

Exactly why this requirement is in place, I don't know, but I guess Chrome's console.log requires the value of this to be console. If you want to store it in a variable, you'll have to bind the value of this:

var l = console.log.bind(console);
like image 199
icktoofay Avatar answered Sep 18 '22 13:09

icktoofay


Node.js console does console.log = console.log.bind(this) in constructor.

like image 24
Esailija Avatar answered Sep 21 '22 13:09

Esailija