On let vs const

栏目: IT技术 · 发布时间: 5年前

内容简介:Myprevious post included this paragraph:This turned out to be very controversial, sparking conversations on Twitter and Reddit. It seems that the majority view (or at least, the most vocally expressed view) is that one should

Myprevious post included this paragraph:

let vs const vs var : Usually you want let . If you want to forbid assignment to this variable, you can use const . (Some codebases and coworkers are pedantic and force you to use const when there is only one assignment.)

This turned out to be very controversial, sparking conversations on Twitter and Reddit. It seems that the majority view (or at least, the most vocally expressed view) is that one should use const wherever possible, only falling back to let where necessary, as can be enforced with the prefer-const ESLint rule.

In this post, I will briefly summarize some of the arguments and counter-arguments I’ve encountered, as well as my personal conclusion on this topic.

Why prefer-const

  • One Way to Do It : It is mental overhead to have to choose between let and const every time. A rule like “always use const where it works” lets you stop thinking about it and can be enforced by a linter.
  • Reassignments May Cause Bugs : In a longer function, it can be easy to miss when a variable is reassigned. This may cause bugs. Particularly in closures, const gives you confidence you’ll always “see” the same value.
  • Learning About Mutation : Folks new to JavaScript often get confused thinking const implies immutability. However, one could argue that it’s important to learn the difference between variable mutation and assignment anyway, and preferring const forces you to confront this distinction early on.
  • Meaningless Assignments : Sometimes, an assignment doesn’t make sense at all. For example, with React Hooks, the values you get from a Hook like useState are more like parameters. They flow in one direction. Seeing an error on their assignment helps you learn earlier about the React data flow.
  • Performance Benefits : There are occasional claims that JavaScript engines could make code using const run faster due to the knowledge the variable won’t be reassigned.

Why Not prefer-const

  • Loss of Intent : If we force const everywhere it can work, we lose the ability to communicate whether it was important for something to not be reassigned.
  • Confusion with Immutability : In every discussion about why you should prefer const , someone always confuses with immutability. This is unsurprising, as both assignment and mutation use the same = operator. In response, people are usually told that they should “just learn the language”. However, the counter-argument is that if a feature that prevents mostly beginner mistakes is confusing to beginners, it isn’t very helpful. And unfortunately, it doesn’t help prevent mutation mistakes which span across modules and affect everyone.
  • Pressure to Avoid Redeclaring : A const -first codebase creates a pressure to not use let for conditionally assigned variables. For example, you might write const a = cond ? b : c instead of an if condition, even if both b and c branches are convoluted and giving them explicit names is awkward.
  • Reassignments May Not Cause Bugs : There are three common cases when reassignments cause bugs: when the scope is very large (such as module scope or huge functions), when the value is a parameter (so it’s unexpected that it would be equal to something other than what was passed), and when a variable is used in a nested function. However, in many codebases most variables won’t satisfy either of those cases, and parameters can’t be marked as constant at all.
  • No Performance Benefits : It is my understanding that the engines are already aware of which variables are only assigned once — even if you use var or let . If we insist on speculating, we could just as well speculate that extra checks can create performance cost rather than reduce it. But really, engines are smart.

My Conclusion

I don’t care.

I would use whatever convention already exists in the codebase.

If you care, use a linter that automates checking and fixing this so that changing let to const doesn’t become a delay in code review.

Finally, remember that linters exist to serve you . If a linter rule annoys you and your team, delete it. It may not be worth it. Learn from your own mistakes.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C语言的科学和艺术

C语言的科学和艺术

罗伯茨 / 翁惠玉 / 机械工业出版社 / 2005-3 / 55.00元

《C语言的科学和艺术》是计算机科学的经典教材,介绍了计算机科学的基础知识和程序设计的专门知识。《C语言的科学和艺术》以介绍ANSI C为主线,不仅涵盖C语言的基本知识,而且介绍了软件工程技术以及如何应用良好的程序设计风格进行开发等内容。《C语言的科学和艺术》采用了库函数的方法,强调抽象的原则,详细阐述了库和模块化开发。此外,《C语言的科学和艺术》还利用大量实例讲述解决问题的全过程,对开发过程中常见......一起来看看 《C语言的科学和艺术》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具