【问题标题】:Why can't I access global-scope variables from one file in another file?为什么我不能从另一个文件中的一个文件访问全局范围变量?
【发布时间】:2020-06-12 17:35:09
【问题描述】:

假设我的 HTML 正文中有以下代码:

<script src = "first.js"></script>
<script src = "second.js"></script>

所以在 first.js 中,我在全局范围内声明了两个变量。为什么我不能从 second.js 访问它们?

【问题讨论】:

    标签: javascript variables global-scope


    【解决方案1】:

    以下代码按预期工作。如果无法查看所有相关代码,就无法诊断您的问题。这就是你被否决的原因。

    index.html

    <script src="first.js"></script>
    <script src="second.js"></script>
    <script>
      console.log(firstGlobal, secondGlobal); // true true
    </script>
    

    first.js

    var firstGlobal = true;
    

    second.js

    var secondGlobal = true;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-30
      • 2020-08-31
      • 2015-12-28
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      相关资源
      最近更新 更多