我可以在javascript中扩展控制台对象(用于重新路由日志记录)吗? [英] Can I extend the console object (for rerouting the logging) in javascript?

查看:107
本文介绍了我可以在javascript中扩展控制台对象(用于重新路由日志记录)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以扩展控制台对象?

Is it possible to extend the console object?

我尝试过类似的事情:

Console.prototype.log = function(msg){
    Console.prototype.log.call(msg);
    alert(msg);
}

但这不起作用。
我想通过像 log4javascript 这样的框架向控制台对象添加额外的日志记录,并且仍然使用标准控制台对象(在我的代码中没有log4javascript的情况下)。

But this didn't work. I want to add additional logging to the console object via a framework like log4javascript and still use the standard console object (in cases where log4javascript is not available) in my code.

提前致谢!

推荐答案

尝试以下内容:

(function() {
    var exLog = console.log;
    console.log = function(msg) {
        exLog.apply(this, arguments);
        alert(msg);
    }
})()

这篇关于我可以在javascript中扩展控制台对象(用于重新路由日志记录)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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