问题描述
我的理解是一个棱角分明的最佳实践使用$登录代替的console.log。
但是,我无法找到良好的文档说明具体原因。为什么要开发利用$登录?
I understand it is a best practice in angular to use $log instead of console.log.However, I can't find good documentation explaining the reasons. Why should a developer use $log?
推荐答案
$日志
首先检查浏览器是否支持的console.log
(IE 8,例如,没有)。被显示在IE 8注意此prevents错误:这并不意味着它会记录什么的IE 8,它只是意味着它不会抛出错误
$log
first checks if the browser supports console.log
(IE 8, for example, doesn't). This prevents errors being displayed on IE 8. Note: this doesn't mean it will log anything on IE 8, it simply means it won't throw the error.
旁边的是,它也可以让你和模拟 $日志
用于扩展和测试目的,如果你是这样的倾向。例如,你可以装点它登录为IE 8支持的阵列。
Next to that, it also allows you to decorate and mock $log
for extending and testing purposes, if you are so inclined. You could for example decorate it to log to an array for IE 8 support.
一个奖金功能:如果你传递一个JavaScript 错误
实例,它会尝试很好地格式化。这可以通过阅读源$ C $ C 一被发现>。
A bonus feature: if you pass it a JavaScript Error
instance, it will attempt to format it nicely. This can be found out by reading the source code.
编辑:这并不是说IE 8不支持的console.log它只是直到开发者工具打开不创建控制台的对象。请参阅下面的评论更多的细节。
"It is not that IE 8 doesn't support console.log. It just doesn't create the console object until the dev tools are opened." See comments below for more details.
这篇关于为什么要使用角度的$登录代替的console.log?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!