Javascript 全局变量正在重置

Javascript Global Variables Are Resetting

我的内容摘要"M TRYING TO DO: I've put together a very simple game that uses a do/while function and switch to check what room you are in. If you are in room = 1, the switch selects room1 and runs the room1() function. Inside the room, you can choose which direction to go. If you choose north or east, a door1() or door4() function will run to say "你想打开这扇门吗?”你可以说 "Open" 然后走进下一个房间,你的房间 = 1 将更新为一个新值。

什么是坏的:这一切都很好地工作,功能(虽然它们有些臃肿)似乎在正常工作。一个主要问题是,每当我走进一扇门时,我的变量都会重置,所以我总是在房间 1,我的指南针总是等于 0,门或房间的 none 等于 "visited"(例如: 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(compass,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

如果我去 'north,',指南针将更新以说明我从哪个方向观察房间,door4(room,door4V) 将 运行.

    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 函数应该说,"Oh! Since room = 4, you are now in room 4." 但事实并非如此。 房间确实 = 4,但是当 do/while 重新 运行 时,我回到了房间 1,并且所有变量似乎都已重置。

你的变量是 "resetting" 的原因是因为 JavaScript 中的原语是按值传递的。在您的 do-while 循环中,您将变量 room1V .. room4V 传递给函数 room1 .. room4。 JavaScript 中的变量是原语,按值传递。这意味着在被调用函数中对它们所做的任何更改都不会反映在原始参数中。当您使用 compassdoor4v 调用 door4 函数时也是如此:这些变量按值传递。为了更好地理解这一点,请尝试阅读 this chapter.

您 运行 遇到了 "variable shadowing" (Variable shadowing in 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 while 循环中更新您的房间:

// 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);