我想修改 console.log
,以便它使用 console.log
保存应用程序输出到命令行的所有内容。
我已经尝试过
var log = console.log;
console.log = function () {
// fs.appendFile('log.txt ..
log.apply(log, arguments);
}
但是它给了我错误
Illegal invocation
最佳答案
apply
的第一个参数是 this
将引用的内容。要模拟对 console.log()
的调用,您必须传递 console
,而不是函数本身:
var log = console.log;
log.apply(console, arguments);
// log.apply(this, arguments); would work as well
关于javascript - 修改console.log,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30788946/