【发布时间】:2016-06-03 13:14:37
【问题描述】:
我想重写console.log 方法以在调用console.log 时调用一组任务。我参考了其他 Stackoverflow 答案,但这给了我错误:
未捕获的 RangeError:超出最大调用堆栈大小。
这就是我想做的:
backupconsolelog = console.log;
console.log = function(arguments)
{
//do my tasks;
backupconsolelog(arguments);
}
更新 1:不知何故成功地覆盖了 console.log,但我现在无法在完成覆盖的同一个 .js 文件中执行 console.log(displaySomethingInConsole)。这以某种方式导致对console.log 的递归调用,并再次给出Uncaught RangeError: Maximum call stack size exceeded.
如何在同一个 .js 文件中使用 console.log()?
更新 2:我有一个 check() 函数,它被覆盖的 console.log 调用。但是,check() 函数内部有一个console.log 调用导致Maximum call stack size exceeded. 错误。
更新 3:又出错了! :(
未捕获的类型错误:非法调用
var _log = window.console.log;
window.console.log = function () {
_log.apply(this, arguments);
check();
};
_log("aaa");
check() {};
更新 4:Bindingconsole 到 _log,即console.log.bind(console) 清除它。
【问题讨论】:
标签: javascript overriding console.log