离开“console.log()"是个坏主意;在您的生产 JavaScript 代码中调用? [英] Bad idea to leave "console.log()" calls in your production JavaScript code?

查看:28
本文介绍了离开“console.log()"是个坏主意;在您的生产 JavaScript 代码中调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 JavaScript 中有一堆 console.log() 调用.

I have a bunch of console.log() calls in my JavaScript.

我应该在部署到生产环境之前将它们注释掉吗?

Should I comment them out before I deploy to production?

我只想将它们留在那里,这样如果我需要进行更多调试,我就不必费心重新添加注释.这是个坏主意吗?

I'd like to just leave them there, so I don't have to go to the trouble of re-adding the comments later on if I need to do any more debugging. Is this a bad idea?

推荐答案

它会导致 Javascript 错误,终止包含错误的 Javascript 块的执行.

It will cause Javascript errors, terminating the execution of the block of Javascript containing the error.

但是,您可以定义一个在 Firebug 未激活时无操作的虚拟函数:

You could, however, define a dummy function that's a no-op when Firebug is not active:

if(typeof console === "undefined") {
    console = { log: function() { } };
}

如果您使用除 log 之外的任何方法,您也需要将它们存根.

If you use any methods other than log, you would need to stub out those as well.

这篇关于离开“console.log()"是个坏主意;在您的生产 JavaScript 代码中调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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