ecmascript-6 – 我为什么要在函数内部使用let而不是const?

前端之家收集整理的这篇文章主要介绍了ecmascript-6 – 我为什么要在函数内部使用let而不是const?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的项目中为js(es6)添加了linters,对于新配置,我发现它们阻止使用const内部函数 – 仅用于模块级常量.内部函数我应该使用let.但我无法找到这种规则的任何理由.为什么?

对于jscs来说就是这样

disallowConstOutsideModuleScope: const should only be used in module scope (not inside functions/blocks)

我知道我可以配置和关闭该规则,我只是想知道它启用了什么?这种检查的动机是什么?

附:我有链接https://madhatted.com/2016/1/25/let-it-be与块“常量常量”

There is another school of thought on when to use let and const I’ll need to address. This strategy suggests developers use const as much as possible. Any variable that is not re-assigned should be declared with const.

I think this usage is poor practice. It adds an extra distraction to the process of programming,and results in code difficult to understand and change.

但我发现这些论点很有价值

解决方法

这只是一个编码指南.他们遵循 this学院的思想.如果您不想使用它,请随时在.jscsrc文件中将其关闭.要点是:
  1. Aggressive use of const devalues the operator
  2. Choosing const first really means choosing to think about every declaration. Will the next line of code change this assignment? How
    will I use this variable?
  3. There is no known performance difference between let and const.
原文链接:https://www.f2er.com/js/157522.html

猜你在找的JavaScript相关文章