Javascript全局变量正在重置



我尝试做的事情摘要:我制作了一个非常简单的游戏,它使用DO/while功能和开关来检查你在哪个房间。如果您在房间=1中,则开关会选择房间1并运行房间1()函数。在房间里,你可以选择去哪个方向。如果你选择北或东,door1()或door4()函数会运行,说"你想打开这扇门吗?"你可以说"打开",然后走进下一个房间,你的room=1将更新为一个新值。

损坏的地方:这一切都运行得很好,功能(虽然有些臃肿)似乎也在正常工作。一个主要问题是,每当我进门时,我的变量都会重置,所以我总是在1号房间,我的指南针总是等于0,没有一扇门或房间等于"已访问"(例如:room1V=0)。

基本上,我穿过一扇门,又回到了1号房间,而不是2号或4号房间。

这是代码和演练:

    var room1V = 0; //room1  // these variables tell the computer whether I have 'visited' a room before.
    var room2V = 0; //room2
    var room3V = 0; //room3
    var room4V = 0; //room4
    var door1V = 0; //room1 - room2 // these variables tell the computer whether I have used a door before.
    var door2V = 0; //room2 - room3
    var door3V = 0; //room3 - room4
    var door4V = 0; //room4 - room1
    var compass = 0; // which side of the room am I on?
    var room = 1; // what room am I currently in?
    var reply = 1; // this is re-declared as a local variable in each function, and it works fine
    win = 0; // this will eventually tell the room-check do/while to stop
    // This do/while checks what room I am in:
    do {
        quit = 0;
        switch(room) {
            case '1':
                room1(compass,room1V);
                break;
            case '2':
                room2(compass,room2V);
                break;
            case '3':
                room3(compass,room3V);
                break;
            case '4':
                room4(compass,room4V);
                break;
        }
    } while (win != 1);

由于默认值为room=1,room1(指南针,room1V)功能将启动。

    function room1(compass,room1V) {
        if (room1V === 1) {
            console.log("You are in room 1 again.");
            document.write("You are in room 1 again." + "<br>");
            var reply = prompt("Where would you like to go? EAST, NORTH?");
            switch(reply.toLowerCase()) {
                case 'east':
                    compass = "east"; //because you are trying to open the east door, you will now see room1 from the east. If you make it through, your compass will update to 'west' because you will be on the west side of room2.
                    door1(compass,door1V);
                    break;
                case 'north':
                    compass = "north";
                    door4(compass,door4V);
                    break;
                default:
                    console.log("Something went wrong.");
                    document.write("Something went wrong." + "<br>");
            }
        } else {
            console.log("You are in room 1.");
            document.write("You are in room 1." + "<br>");
            room1V = 1;
            reply = prompt("Where would you like to go? EAST, NORTH?");
            switch(reply.toLowerCase()) {
                case 'east':
                    compass = "east";
                    door1(compass,door1V);
                    break;
                case 'north':
                    compass = "north";
                    door4(compass,door4V);
                    break;
                default:
                    console.log("Something went wrong.");
                    document.write("Something went wrong." + "<br>");
            }
        }
    }   // Working

如果我去"北方",指南针会更新,告诉我从哪个方向看房间,门4(房间,门4V)就会运行。

    function door4(room,door4V) {
        if (door1V === 1) {
            console.log("You approach door 4 again.");
            document.write("You approach door 4 again." + "<br>");
            var reply = prompt("What would you like to do? OPEN, QUIT?");
            switch(reply.toLowerCase()) {
                case 'open':
                    if (room === 4) {
                       room = 1;
                       compass = "north";
                       console.log("You walk through door 4 into room 1...");
                       document.write("You walk through door 4 into room 1..." + "<br>");
                    } else {
                       room = 4;
                       compass = "south";
                       console.log("You walk through door 4 into room 4...");
                       document.write("You walk through door 4 into room 4..." + "<br>");
                       }
                    quit = 1;
                    break;
                case 'quit':
                    quit = 1;
                    break;
                default:
                    console.log("Something went wrong.");
                    document.write("Something went wrong." + "<br>");
            }
        } else {
            console.log("You approach door 4.");
            document.write("You approach door 4." + "<br>");
            var reply = prompt("What would you like to do? OPEN, QUIT?");
            switch(reply.toLowerCase()) {
                case 'open':
                    if (room === 4) {
                       room = 1;
                       compass = "north";
                       console.log("You walk through door 4 into room 1...");
                       document.write("You walk through door 4 into room 1..." + "<br>");
                    } else {
                       room = 4;
                       compass = "south";
                       console.log("You walk through door 4 into room 4...");
                       document.write("You walk through door 4 into room 4..." + "<br>");
                    }
                    quit = 1;
                    break;
                case 'quit':
                    quit = 1;
                    break;
                default:
                      console.log("Something went wrong.");
                      document.write("Something went wrong." + "<br>");
            }
        }
    }       // Working

在这一点上,do/while函数应该说,"哦!既然房间=4,你现在就在房间4。"但事实并非如此。房间do=4,但当do/while重新运行时,我回到房间1,所有变量似乎都重置了。

变量"重置"的原因是JavaScript中的基元是按值传递的。在do-while循环中,传递变量room1V。。room4V到函数room1。。room4。变量是JavaScript中的基元,并通过值传递。这意味着在被调用函数中对它们所做的任何更改都不会反映在原始参数中。使用compassdoor4v调用door4函数时也是如此:这些变量是按值传递的。要更好地理解这一点,请尝试阅读本章。

您遇到了"可变阴影"(JavaScript中的可变阴影)问题

var fancy = 0;
function room1(fancy) {
  /* here, there are two variables with the name 'fancy' in scope */
  fancy = 1;
}
console.log(fancy); // 0
room1();
console.log(fancy); // 0

您可以通过将room1参数更改为具有不同名称来解决此问题:

function room1(otherFancy) { fancy = 1; }现在:

console.log(fancy); // 0
room1();
console.log(fancy); // 1

您永远不会更改房间变量的值以匹配新房间。它在全局变量中设置,并且不会更改。

在你的do-white循环中更新你的房间,如下所示:

// This do/while checks what room I am in:
    do {
        quit = 0;
        switch(room) {
            case '1':
                room1(compass,room1V);
                room = 1;
                break;
            case '2':
                room2(compass,room2V);
                room = 2;
                break;
            case '3':
                room3(compass,room3V);
                room = 3;
                break;
            case '4':
                room4(compass,room4V);
                room = 4;
                break;
        }
    } while (win != 1);

相关内容

  • 没有找到相关文章

最新更新