<bdo id='aMvaf'></bdo><ul id='aMvaf'></ul>
<legend id='aMvaf'><style id='aMvaf'><dir id='aMvaf'><q id='aMvaf'></q></dir></style></legend>
  • <i id='aMvaf'><tr id='aMvaf'><dt id='aMvaf'><q id='aMvaf'><span id='aMvaf'><b id='aMvaf'><form id='aMvaf'><ins id='aMvaf'></ins><ul id='aMvaf'></ul><sub id='aMvaf'></sub></form><legend id='aMvaf'></legend><bdo id='aMvaf'><pre id='aMvaf'><center id='aMvaf'></center></pre></bdo></b><th id='aMvaf'></th></span></q></dt></tr></i><div id='aMvaf'><tfoot id='aMvaf'></tfoot><dl id='aMvaf'><fieldset id='aMvaf'></fieldset></dl></div>

    <small id='aMvaf'></small><noframes id='aMvaf'>

    1. <tfoot id='aMvaf'></tfoot>

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

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

        <i id='sIpz8'><tr id='sIpz8'><dt id='sIpz8'><q id='sIpz8'><span id='sIpz8'><b id='sIpz8'><form id='sIpz8'><ins id='sIpz8'></ins><ul id='sIpz8'></ul><sub id='sIpz8'></sub></form><legend id='sIpz8'></legend><bdo id='sIpz8'><pre id='sIpz8'><center id='sIpz8'></center></pre></bdo></b><th id='sIpz8'></th></span></q></dt></tr></i><div id='sIpz8'><tfoot id='sIpz8'></tfoot><dl id='sIpz8'><fieldset id='sIpz8'></fieldset></dl></div>

            <legend id='sIpz8'><style id='sIpz8'><dir id='sIpz8'><q id='sIpz8'></q></dir></style></legend>
          • <small id='sIpz8'></small><noframes id='sIpz8'>

              <tfoot id='sIpz8'></tfoot>
              • <bdo id='sIpz8'></bdo><ul id='sIpz8'></ul>
                  <tbody id='sIpz8'></tbody>

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                  How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)
                    <legend id='xXrrU'><style id='xXrrU'><dir id='xXrrU'><q id='xXrrU'></q></dir></style></legend>

                    <small id='xXrrU'></small><noframes id='xXrrU'>

                    • <bdo id='xXrrU'></bdo><ul id='xXrrU'></ul>

                          <i id='xXrrU'><tr id='xXrrU'><dt id='xXrrU'><q id='xXrrU'><span id='xXrrU'><b id='xXrrU'><form id='xXrrU'><ins id='xXrrU'></ins><ul id='xXrrU'></ul><sub id='xXrrU'></sub></form><legend id='xXrrU'></legend><bdo id='xXrrU'><pre id='xXrrU'><center id='xXrrU'></center></pre></bdo></b><th id='xXrrU'></th></span></q></dt></tr></i><div id='xXrrU'><tfoot id='xXrrU'></tfoot><dl id='xXrrU'><fieldset id='xXrrU'></fieldset></dl></div>
                              <tbody id='xXrrU'></tbody>

                          • <tfoot id='xXrrU'></tfoot>