console.log()在控制台以外的对象上调用 [英] console.log() called on object other than console

查看:176
本文介绍了console.log()在控制台以外的对象上调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得当我想将 console.log 作为回调参数传递给某个函数时,除非我使用 bind()将 console 绑定到它的方法。

I remember that always when I wanted to pass console.log as a callback parameter to some function, it didn't work unless I used the bind() method to bind console to it.

例如:

const callWithTest = callback => callback('test');
callWithTest(console.log); // That didn't use to work.
callWithTest(console.log.bind(console)); // That worked (and works) fine.

请参阅未捕获的TypeError:javascript中的非法调用

但是,最近我注意到 console.log()即使在控制台以外的对象上调用时也能正常工作。例如:

However, recently I noticed that console.log() works fine even when called on object other than console. For example:

console.log.call(null, 'test');

log 'test'

何时以及为何会改变?规范是否说明了什么?

When and why did it change? Does the specification say anything about it?

推荐答案

编辑器的控制台API草案曾经说过:


记录API应该都是可调用的函数,允许它们作为参数传递给错误处理回调,forEach方法等。

Logging APIs SHOULD all be callable functions allowing them to be passed as arguments to error handling callbacks, forEach methods, etc.

这不再包含在规范的当前版本

我认为Chrome和Node.js改变了它的工作方式,就像在规范中一样,但它似乎在它之前就像那样工作。

I thought that Chrome and Node.js changed it to work like in the specification, but it seems that it worked like that even before it.

我还好奇它什么时候改变了,原因是什么。

I'm still curious when did it change and what was the reason of that.

这篇关于console.log()在控制台以外的对象上调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆