【发布时间】:2017-01-22 19:33:37
【问题描述】:
我记得总是当我想将console.log 作为回调参数传递给某个函数时,除非我使用bind() 方法将console 绑定到它,否则它不起作用。
例如:
const callWithTest = callback => callback('test');
callWithTest(console.log); // That didn't use to work.
callWithTest(console.log.bind(console)); // That worked (and works) fine.
见Uncaught TypeError: Illegal invocation in javascript。
但是,最近我注意到 console.log() 即使在控制台以外的对象上调用时也能正常工作。例如:
console.log.call(null, 'test');
记录'test'。
它何时以及为何改变?规范有什么说明吗?
【问题讨论】:
-
顺便说一句,Opera(旧版)自古就有这个...
标签: javascript this console.log function-binding