简体   繁体   English

JavaScript - 如何在.js文件之间传递全局变量?

[英]JavaScript - How can you pass global variables between .js files?

I'm pretty new to JavaScript, and programming in general. 我对JavaScript和编程很新。 I think I'm going about this the wrong way but I'll try to explain what I'm trying to do and how I'm doing it. 我想我会以错误的方式解决这个问题,但我会尝试解释我正在尝试做什么以及我是如何做的。 It's not working. 它不起作用。

I made a page with five boxes that you can click. 我创建了一个包含五个框的页面,您可以单击它。 When you click one, it will run a function stored in one js file and look through cases to find the one that matches, then change the text in the text box as well as the choices in the five boxes below. 单击一个时,它将运行存储在一个js文件中的函数,并查看案例以查找匹配的函数,然后更改文本框中的文本以及下面五个框中的选项。 I think I'm having trouble with global variables not carrying over from one included .js file to another. 我认为我遇到的问题是全局变量没有从一个包含的.js文件转移到另一个.js文件。

I can't think of another way of doing this. 我想不出另一种方法。 I can't just return a single variable since I need to change around 5 separate variables for each case statement for each choice. 我不能只返回一个变量,因为我需要为每个选项的每个case语句更改大约5个单独的变量。 Is there a better way of carrying this information back and forth than these $choice variables that don't seem to work? 有没有比这些看似无效的$选择变量来回传递这些信息更好的方法?

file 1.js 档案1.js

var $choice1 = "0";
var $choice2 = "0";
var $choice3 = "0";
var $choice4 = "0";
var $choice5 = "0";

//Main Body - start doing stuff once the page has loaded    
$( document ).ready(function() {    

//Run once
NewArea("newgame");

$("#choice1").click(function() {
    NewArea($choice1);
    });

$("#choice2").click(function() {
    NewArea($choice2);
    });

$("#choice3").click(function() {
    NewArea($choice3);
    });

$("#choice4").click(function() {
    NewArea($choice4);
    });

$("#choice5").click(function() {
    NewArea($choice5);
    });

});

and then we have file 2.js 然后我们有文件2.js

function NewArea($area) {
    switch($area)
    {
    case "newgame": //Start of the game
        $("#textbox").html("blah");
        $("#choice1").html("Look around");
            $choice1 = "look_1";
        $("#choice2").html("Listen around");
            $choice1 = "listen_1";
        $("#choice3").html("Smell around");
            $choice1 = "smell_1";
        $("#choice4").html("Feel around");
            $choice1 = "feel_1";
        $("#choice5").html("Taste around");
            $choice1 = "taste_1";
        break;

    case "look_1":
        $("#textbox").html("blah2");
        $("#choice1").html("");
            $choice1 = "0";
        break;

    default:
        break;
    }
}

use it like this 像这样使用它

<script type='text/javascript' > 
  window.$choice1 = "0";
  window.$choice2 = "0"; 
  window.$choice3 = "0";
  window.$choice4 = "0";
  window.$choice5 = "0";
</script>

then below link file1.js and file2.js 然后在链接file1.jsfile2.js下面

You should at least consider namespacing the global variables for clarity and slightly cleaner code. 您至少应该考虑命名空间全局变量,以获得清晰和稍微清晰的代码。 You can also make it an array rather than discrete variables so you can easily expand them if necessary (bearing in mind your indexing from your ids will be off by 1, since arrays are 0-indexed): 你也可以把它变成一个数组,而不是离散的变量,这样你就可以在必要的时候轻松扩展它们(记住你的id的索引将被关闭1,因为数组是0索引的):

var myGame = {
    choices: [0, 0, 0, 0, 0];
};

As others have mentioned, the key is that this snippet of code loads before your file2.js (either by loading it in a separate file before file1.js and file2.js, or ensuring file1.js loads before file2.js). 正如其他人所提到的,关键是这段代码在file2.js之前加载(通过在file1.js和file2.js之前将其加载到单独的文件中,或者确保file2.js在file2.js之前加载)。

You mention you don't know another way of doing this, and it seems like you're using a DOM-gnostic jQuery approach, so other things that may work (though not necessarily better) are: 你提到你不知道另一种方法,看起来你正在使用DOM-gnostic jQuery方法,所以其他可能有用的东西(虽然不一定更好)是:

  • Storing the choices in hidden input fields. 在隐藏的输入字段中存储选项。 This will clutter your DOM instead of the global namespace. 这将使您的DOM混乱而不是全局命名空间。 ;) So you can have <input type='hidden' value='0' name='choice1Value'/> and then get/set the value in there. ;)所以你可以有<input type='hidden' value='0' name='choice1Value'/>然后在那里获取/设置值。
  • You can also use the jQuery data function to store the value on each choice: $('#choice1').data('choice', 0); 您还可以使用jQuery 数据函数将值存储在每个选项上: $('#choice1').data('choice', 0); for example. 例如。

There are other refactorings you could consider as well, but it depends on the structure of your files and how you load them, but hopefully this provides a few ideas on different approaches. 您还可以考虑其他重构,但这取决于文件的结构以及如何加载它们,但希望这提供了一些关于不同方法的想法。

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

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