[英]Why isn't my JavaScript function able to access global-scope functions/variables defined in my other .js files?
[英]Why can't I access global-scope variables from one file in another file?
假設我的 HTML 正文中有以下代碼:
<script src = "first.js"></script>
<script src = "second.js"></script>
所以在 first.js 中,我在全局 scope 中聲明了兩個變量。 為什么我不能從 second.js 訪問它們?
以下代碼按預期工作。 如果無法查看所有相關代碼,就無法診斷您的問題。 這就是你被否決的原因。
索引.html
<script src="first.js"></script>
<script src="second.js"></script>
<script>
console.log(firstGlobal, secondGlobal); // true true
</script>
第一個.js
var firstGlobal = true;
第二個.js
var secondGlobal = true;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.