• <legend id='wYRh7'><style id='wYRh7'><dir id='wYRh7'><q id='wYRh7'></q></dir></style></legend>

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

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

        在Javascript中有条件地初始化一个常量

        Conditionally initializing a constant in Javascript(在Javascript中有条件地初始化一个常量)
          1. <tfoot id='uLDZ2'></tfoot>
            • <bdo id='uLDZ2'></bdo><ul id='uLDZ2'></ul>
              <i id='uLDZ2'><tr id='uLDZ2'><dt id='uLDZ2'><q id='uLDZ2'><span id='uLDZ2'><b id='uLDZ2'><form id='uLDZ2'><ins id='uLDZ2'></ins><ul id='uLDZ2'></ul><sub id='uLDZ2'></sub></form><legend id='uLDZ2'></legend><bdo id='uLDZ2'><pre id='uLDZ2'><center id='uLDZ2'></center></pre></bdo></b><th id='uLDZ2'></th></span></q></dt></tr></i><div id='uLDZ2'><tfoot id='uLDZ2'></tfoot><dl id='uLDZ2'><fieldset id='uLDZ2'></fieldset></dl></div>
                <tbody id='uLDZ2'></tbody>

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

                <legend id='uLDZ2'><style id='uLDZ2'><dir id='uLDZ2'><q id='uLDZ2'></q></dir></style></legend>

                  本文介绍了在Javascript中有条件地初始化一个常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  从 ES6 开始,我们有 const.

                  ES6 onwards we have const.

                  这是不允许的:

                  const x; //declare first
                  //and then initialize it
                  if(condition) x = 5;
                  else x = 10;
                  

                  这是有道理的,因为它阻止我们在初始化之前使用常量.

                  This makes sense because it prevents us from using the constant before it's initialized.

                  如果我这样做了

                  if(condition)
                      const x = 5;
                  
                  else 
                      const x = 10;
                  

                  x 变为块作用域.

                  那么如何有条件地创建一个常量呢?

                  So how to conditionally create a constant?

                  推荐答案

                  如您所知,您的问题是 const 必须在声明它的相同表达式中初始化.

                  Your problem, as you know, is that a const has to be intialised in the same expression that it was declared in.

                  这并不意味着您分配给常量的值必须是文字值.它实际上可以是任何有效的表达式 - 三元:

                  This doesn't mean that the value you assign to your constant has to be a literal value. It could be any valid expression really - ternary:

                  const x = IsSomeValueTrue() ? 1 : 2;
                  

                  或者也许只是将它分配给一个变量的值?

                  Or maybe just assign it to the value of a variable?

                  let y = 1;
                  if(IsSomeValueTrue()) {
                      y = 2;
                  }
                  
                  const x = y;
                  

                  您当然也可以将其分配给函数的返回值:

                  You could of course assign it to the return value of a function, too:

                  function getConstantValue() {
                      return 3;
                  }
                  
                  const x = getConstantValue();
                  

                  因此,有很多方法可以使值动态化,您只需要确保它只分配在一个地方.

                  So there's plenty of ways to make the value dynamic, you just have to make sure it's only assigned in one place.

                  这篇关于在Javascript中有条件地初始化一个常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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?)
                  • <small id='H15UM'></small><noframes id='H15UM'>

                    • <tfoot id='H15UM'></tfoot>
                        <tbody id='H15UM'></tbody>

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

                          1. <legend id='H15UM'><style id='H15UM'><dir id='H15UM'><q id='H15UM'></q></dir></style></legend>