javascript - console.log method extraction from console - Stack Overflow

Considering that console wasn't overriden and refers to native object, console.log method (and pos

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 patibility?

A significant amount of JS examples (maybe too illustrative) with bound console.log suggests that it may be not.

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 patibility?

A significant amount of JS examples (maybe too illustrative) with bound console.log suggests that it may be not.

Share Improve this question asked May 25, 2016 at 0:49 Estus FlaskEstus Flask 224k79 gold badges472 silver badges612 bronze badges 5
  • Did you try it, does it work? What environment[s] are you running in? – epascarello Commented May 25, 2016 at 0:52
  • That depends on the implementation, but a sane implementation would implement log as something like this._write(log_level, msg, ...), i.e. a shorthand alias to another internal method. As such, this very likely must be preserved. – deceze Commented May 25, 2016 at 0:52
  • @epascarello Never had problems with it in actual FF and Chrome versions, but not sure about the rest. – Estus Flask Commented May 25, 2016 at 0:58
  • @estus: I'm pretty sure in both node and Chrome it was (is?) a problem. In Opera 12 extraction worked. – Bergi Commented May 25, 2016 at 1:43
  • It doesn't even work in Chrome. How can it be 100% safe? – Gokhan Kurt Commented May 28, 2016 at 12:15
Add a ment  | 

2 Answers 2

Reset to default 8

Browsers differ in their console implementations, it appears that WebKit/Blink-based browsers (Chrome, Opera 15+, Safari, etc) are the only ones that are unfortable with extracted console methods. For browser patibility 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.

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

It isn't.

  • In Node.js console methods are bound to instance on method creation. Relevant code in Node.js source.
  • Gecko console method requires valid this (unbound methods are i).

I couldn't find console implementation in Chrome source.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745466370a4628935.html

相关推荐

  • javascript - console.log method extraction from console - Stack Overflow

    Considering that console wasn't overriden and refers to native object, console.log method (and pos

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信