为什么大多数时候我应该在 JavaScript 中使用 const 而不是 let?

2024-04-19前端开发问题
9

本文介绍了为什么大多数时候我应该在 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui tree树组件怎么自定义添加图标
经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l...
2024-10-26 前端开发问题
493

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5