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

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

问题描述

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



我应该评论它们吗?在我部署到生产之前出来?



我想把它们留在那里,所以我不必再去添加评论了如果我需要做更多的调试。这是一个坏主意吗?

解决方案

这将导致Javascript错误,终止包含错误的Javascript块的执行。 / p>

但是,当Firebug未激活时,你可以定义一个虚操作函数:

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

如果您使用 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?

解决方案

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

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

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

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

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

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