Retrieving the console.log method from the console

Given that console not overridden and refers to its own object, the console.log method (and possibly others) is retrieved from the console object using

 var log = obj.log = console.log; // instead of console.log.bind(console) log(...); obj.log(...); 

Is it 100% safe in terms of browser and Node compatibility?

A significant number of JS examples (perhaps too illustrative) with related console.log suggests this may not be the case.

+5
source share
2 answers

Browsers differ in their console implementations, it seems that WebKit / Blink browsers (Chrome, Opera 15+, Safari, etc.) are the only ones that are inconvenient when retrieving console methods. For browser compatibility, the extracted methods must be related:

 var log = console.log.bind(console); 

Node has its own console implementation, which relies on this , but pre-binds its methods . Safely retrieving console methods in Node applications, the same applies to the main Electron process.

NW.js replaces Node console with Chromium's:

Node.js and Chromium have their own setTimeout and console implementations. Currently, for the console, we use the Chromium implementation everywhere because it can print to devtools and provide more information.

It is not safe to retrieve console methods in the context of an NW.js Node.

+8
source

Is it 100% safe in terms of browser and Node compatibility?

This is not true.

I could not find the console implementation in the Chrome source.

+1
source

All Articles