【问题标题】:javascript global variable update not workingjavascript全局变量更新不起作用
【发布时间】:2018-12-04 03:38:29
【问题描述】:

我无法让基本工作正常工作 - 更新函数中全局变量的内容。

所以这里是过度简化的代码示例:

<html>

  <head>
    <script>
      window.mytestip = "Var set as global"; 
      var ConditionVar = 1;

      if (ConditionVar == 1)(function() {
      mytestip = "Var set to Yes";
      });
      else(function() {
        mytestip = "Var set to No";
      });

    </script>
  </head>

  <body>
    <p> <span id=mytest>-</span> </p>
    <script>
      document.getElementById('mytest').innerHTML = window.mytestip;

    </script>
  </body>

</html>

为什么 mytestip 没有更新?

这是一个 jfiddle:https://jsfiddle.net/4bu8gp9f/

分辨率:

添加 () 实际上解决了呈现的代码问题。 但是我的代码是嵌套的,我无法使用它。

相反,我通过设置本地存储变量解决了这个问题,并稍后从本地存储中取回它:

在函数中: localStorage.setItem("LocalIp", mytestip );

稍后在代码中: mytestip = localStorage.getItem("LocalIp");

谢谢大家!

【问题讨论】:

  • elseif 不是函数,不带回调函数。它们是作用域块,
  • if (RTCPeerConnection){ window.mytestip = "Var 设置为 Yes"; } else { window.mytestip = "变量设置为否"; }
  • 如果你要使用IIFE developer.mozilla.org/en-US/docs/Glossary/IIFE,别忘了执行它们。if (ConditionVar == 1)(function() { mytestip = "Var set to Yes"; })(); 注意额外的() 然后结束。
  • 下面的所有答案都去掉了 function(){} 部分 - 问题是更新在函数内不起作用
  • 感谢基思,额外的 () 确实有所作为。让我看看它如何影响完整的代码

标签: javascript variables global


【解决方案1】:

所以,我相信您在 ifs 中使用函数是有充分理由的,所以如果是这种情况,您只是错过了内联函数后面的 '()':

if (ConditionVar == 1) {
    (function() {
        mytestip = "Var set to Yes";
    })();
} else {
    (function() {
        mytestip = "Var set to No";
    })();
}

我添加了括号来组织代码(并建议始终这样做)

如果不是这样,请遵循其他建议的答案。

【讨论】:

  • 正如 Keith 之前添加的,这确实解决了所提供代码中的问题。但是由于某些原因,在完整代码中它会产生错误(如上面的 cmets 所述)
  • 完整代码在嵌套函数中有this,加上()会报错
【解决方案2】:

你应该使用:

if (ConditionVar == 1) {
    mytestip = "Var set to Yes";
} else {
    mytestip = "Var set to No";
}

【讨论】:

    【解决方案3】:

    我认为你的if else statement 是错误的。

    所以我试着这样做。

    <html>
    
      <head>
        <script>
          window.mytestip = "Var set as global";
          var RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
    
          if (RTCPeerConnection){
            window.mytestip = "Var set to Yes";
          }
          else{
            window.mytestip = "Var set to No";
          }
    
        </script>
      </head>
    
      <body>
        <p> <span id=mytest>-</span> </p>
        <script>
          document.getElementById('mytest').innerHTML = window.mytestip;
    
        </script>
      </body>
    
    </html>

    【讨论】:

      【解决方案4】:

      正如丹尼尔和保罗所说的那样,试试这个代码:

      if (RTCPeerConnection) {
          window.mytestip = "Var set to Yes";
       }else 
          window.mytestip = "Var set to No";
       }
      

      别忘了你的 span id 参数上的引号

      【讨论】:

        【解决方案5】:

        分辨率:

        添加 () 实际上解决了呈现的代码问题。但是我的代码是嵌套的,我无法使用它。

        相反,我通过设置本地存储变量解决了这个问题,并稍后从本地存储中取回它:

        在函数中: localStorage.setItem("LocalIp", mytestip );

        稍后在代码中: mytestip = localStorage.getItem("LocalIp");

        谢谢大家!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-14
          • 1970-01-01
          • 2013-07-13
          • 2015-08-12
          • 1970-01-01
          相关资源
          最近更新 更多