【问题标题】:Use JavaScript global variable which is in a function outside function. Keeping in mind that function is assigned to a variable使用在函数外的函数中的 JavaScript 全局变量。请记住,函数已分配给变量
【发布时间】:2017-01-10 22:16:14
【问题描述】:

如何在分配给全局变量tell的函数之外使用全局变量topic。

<html>
<head>
<script>
alert(topic);
tell = function(){
topic = "something";
};
</script>
</head>
<body>
</body>
</html>

最终我想在函数之外的任何地方使用 topic_splitted 的值。

client.onMessageArrived = function (message) {
        topic_splitted = message.destinationName.split("/");
        console.log("Topic Splitted: " + topic_splitted);
        console.log("Message Topic: " + message.destinationName);
        console.log("Message Arrived: " + message.payloadString);
        if (message.destinationName == "in_progress"){
            if (message.payloadString == "false") {
                stepCode();
            }
        }

        var new_data = JSON.parse(message.payloadString);

        $.extend(true, data, data, new_data);
    };

【问题讨论】:

  • 主题必须在警报之前定义,并通过var topic; 运行,然后才能使用
  • 您需要在某处调用tell() 来分配变量
  • 在函数外定义topic_splitted,在使用它的函数范围内。
  • @Traktor53 我没明白你的意思抱歉。你能解释一下这将是一个很大的帮助。非常感谢
  • 关于您使用onMessageArrived 监听器进行的编辑,您根本不应该使用全局变量。 Use callbacks instead 传递结果。

标签: javascript html scope global-variables


【解决方案1】:

在第一个帖子示例的tell 函数中使用未声明的变量不是一个好主意,例如topic

如果您为未声明的变量赋值,JavaScript 会为您创建一个全局变量,除非代码在严格模式下运行。在严格模式下,它采用更安全的方法,即抛出未定义变量的错误。

在哪里声明公共变量?

如果在页面加载时需要在全局级别执行的代码访问该变量,则需要在全局级别声明它。这被认为是不可取的,应尽可能避免。

另一种方法是在函数内声明变量,该函数可能会立即被调用(IIFE)或响应窗口加载或文档就绪事件而执行。在此类函数中声明的变量可以通过外部函数中的代码访问以及嵌套在其中的所有函数。

因此,如果topic_splitted 是在所有使用它的函数之外定义的,最好是在一个通用的外部函数中,应该没有问题。

【讨论】:

    【解决方案2】:

    看起来您实际上并没有运行函数“tell”。试试这个:

    <html>
      <head>
        <script>
          var topic;
          var tell = function(){
            topic = "something";
          };
          tell()
          alert(topic);
        </script>
       </head>
      <body>
      </body>
    </html>
    

    【讨论】:

    • 我想在函数之前使用主题变量&lt;html&gt; &lt;head&gt; &lt;script&gt; var topic; tell() alert(topic); var tell = function(){ topic = "something"; }; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt;
    • 然后正确订购您的东西
    • @ShawnikRaghav 在设置变量之前,您不能调用存储在变量中的函数:变量声明被提升,但设置它们的值发生在它被写入的地方。在设置之前,提升变量的值为 undefined 尝试使用命名函数:function tell() { //body}
    • @Traktor53 这是原始代码,我想使用 topic_splitted client.onMessageArrived = function (message) { topic_splitted = message.destinationName.split("/"); console.log("Topic Splitted: " + topic_splitted); console.log("Message Topic: " + message.destinationName); console.log("Message Arrived: " + message.payloadString); if (message.destinationName == "in_progress"){ if (message.payloadString == "false") { stepCode(); } } var new_data = JSON.parse(message.payloadString); $.extend(true, data, data, new_data); };上的值
    • 那段代码并不适合评论——而且我没有立即看出它与问题的关系。请编辑问题,以便更好地解释问题所在以及出了什么问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多