Reputation: 222855
Considering that console
wasn't overriden and refers to native object, console.log
method (and possibly others) is extracted from console
object with
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 amount of JS examples (maybe too illustrative) with bound console.log
suggests that it may be not.
Upvotes: 5
Views: 1139
Reputation: 5316
Is it 100% safe in terms of browser and Node compatibility?
It isn't.
this
(unbound methods are i).I couldn't find console
implementation in Chrome source.
Upvotes: 1
Reputation: 222855
Browsers differ in their console
implementations, it appears that WebKit/Blink-based browsers (Chrome, Opera 15+, Safari, etc) are the only ones that are uncomfortable with extracted console
methods. For browser compatibility extracted methods have to be bound:
var log = console.log.bind(console);
Node has its own console
implementation that relies on this
but pre-binds its methods. It is safe to extract console
methods in Node applications, the same applies to Electron's main process.
NW.js replaces Node console
with Chromium's:
Node.js and Chromium each has its own implementation of setTimeout and console. Currently, for console, we use Chromium's implementation everywhere, because it can print in devtools and have more information exposed.
It is not safe to extract console
methods in NW.js Node's context.
Upvotes: 8