我应该从生产代码中删除console.log吗? [英] Should I be removing console.log from production code?
问题描述
我目前在我的代码中到处都有这个JS语句:
I currently have this JS statement everywhere in my code:
window.console && console.log("Foo");
我想知道这是否成本高昂,或者在生产中是否有任何负面影响。
I am wondering if this is costly at all, or has any negative side-effects in production.
我可以自由离开客户端登录,还是应该离开?
Am I free to leave client-side logging in, or should it go?
编辑:最后,我想最好的论点是我(以及其他任何人?)可以提出的是,通过留下记录消息,在服务器和客户端之间传输的数据可能是不可忽略的。如果生产代码是完全优化后,必须删除日志记录以减少发送到客户端的javascript的大小。
In the end, I suppose the best argument I (and anyone else?) can come up with is that there is a possibly non-negligible amount of extra data transferred between the server and the client by leaving logging messages left in. If production code is to be fully optimized, logging will have to be removed to reduce the size of javascript being sent to the client.
推荐答案
你应该不将开发工具添加到生产页面。
You should not add development tools to a production page.
回答其他问题:代码不会产生负面影响:
To answer the other question: The code cannot have a negative side-effect:
-
window.console
如果console $ c $将评估为false c>未定义
-
console.log(Foo)
将消息打印到th定义它时的控制台(假设页面没有用非函数覆盖console.log
)。
window.console
will evaluate to false ifconsole
is not definedconsole.log("Foo")
will print the message to the console when it's defined (provided that the page does not overwriteconsole.log
by a non-function).
这篇关于我应该从生产代码中删除console.log吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!