简体   繁体   English

为什么我不能从另一个文件中的一个文件访问全局范围变量?

[英]Why can't I access global-scope variables from one file in another file?

Say I have the following code in my HTML body:假设我的 HTML 正文中有以下代码:

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

So in first.js I have two variables declared in a global scope.所以在 first.js 中,我在全局 scope 中声明了两个变量。 Why can't I access them from second.js?为什么我不能从 second.js 访问它们?

The following code works as expected.以下代码按预期工作。 Without being able to see all your relevant code it's impossible to diagnose your issue.如果无法查看所有相关代码,就无法诊断您的问题。 That's why you're being downvoted.这就是你被否决的原因。

index.html索引.html

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

first.js第一个.js

var firstGlobal = true;

second.js第二个.js

var secondGlobal = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM