为什么大多数时候我应该在 JavaScript 中使用 const 而不是 let? [英] Why most of the time should I use const instead of let in JavaScript?

查看:49
本文介绍了为什么大多数时候我应该在 JavaScript 中使用 const 而不是 let?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么大多数时候我应该在 JavaScript 中使用 const 而不是 let?正如我们所知,如果我们使用 const 那么我们以后不能重新分配值.那为什么不使用 let 而不是 const 呢?

Why most of the time should I use const instead of let in JavaScript? As we know if we use const then we can't reassign value later. Then why not use let instead of const?

推荐答案

基本上,

  • 使用 let 如果变量的值会在代码中发生变化
  • 使用 const 如果它不会并且您/您的团队希望在您正在工作的项目中的这些情况下使用 const在;这是风格问题
  • use let if the variable's value will change during the code
  • use const if it won't and you / your team want to use const in those situations in the project you're working on; it's a matter of style

如果您确实使用 const,那么令人惊讶的是,上面的指导方针意味着您使用 const 的频率,因为您最终不需要更改变量的值(如果您遵循保持功能合理大小等的常规规则).(好吧,无论如何,这让我感到惊讶......)

If you do use const, then it's surprising how often it turns out that the guidelines above mean you use const because you end up not needing to change a variable's value (if you're following the usual rules of keeping your functions of reasonable size and such). (Well, it surprised me, anyway...)

当变量的¹值不打算改变时使用 const 可以完成一些事情:

Using const when the variable's¹ value is not meant to change accomplishes a few things:

  1. 它告诉其他阅读您的代码的人您不打算更改该值.

  1. It tells others reading your code that you don't intend the value to change.

如果您更改代码以便写入该变量,它会给您一个很好的主动错误.(一个不错的 IDE 可以主动标记它,但如果没有,您将在运行代码时收到错误.)然后您可以做出明智的、有意识的决定:您是否应该将其更改为 let,还是您一开始不是要更改该变量的值?

It gives you a nice proactive error if you change the code so it writes to that variable. (A decent IDE can flag this up proactively, but if not, you'll get the error when running the code.) You can then make an informed, intentional decision: Should you change it to let, or did you not mean to change that variable's value in the first place?

它向 JavaScript 引擎的优化器提示您不会更改该变量的值.虽然引擎可以经常通过代码分析来解决这个问题,但使用 const 可以省去麻烦.(警告:我不知道这对 JavaScript 引擎是否真的有用.看起来确实有用,但运行时代码优化是一个非常复杂且有时不直观的过程.)

It gives a hint to the JavaScript engine's optimizer that you won't be changing that variable's value. While the engine can frequently work that out through code analysis, using const saves it the trouble. (Caveat: I have no idea whether this is actually useful to the JavaScript engine. It seems like it would be, but runtime code optimization is a very complicated and sometimes non-intuitive process.)

<小时>

¹ 是的,使用术语变量"来指代定义上不变的东西很有趣.:-) 规范中的术语是绑定",但我敢打赌,你不会很快听到人们在日常对话中谈论绑定"……所以总的术语可能会保持可变",除非我们可以特别提及某种常数".


¹ Yes, it's funny to use the term "variable" to refer to something that by definition doesn't vary. :-) The specification's term is "binding," but I bet you won't hear people talking about "bindings" in everyday conversation anytime soon... So the aggregate term will probably remain "variable" except when we can specifically refer to something as a "constant."

这篇关于为什么大多数时候我应该在 JavaScript 中使用 const 而不是 let?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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