我记得总是当我想作为回调参数传递给某个函数时,除非我使用该方法绑定到它,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'
。
何时以及为何改变?规范是否对此有任何说明?