在Elevator Saga学习工具中显示为未定义的javascript变量

时间:2016-01-13 22:38:34

标签: javascript arrays scoping

我是JavaScript的新手,来自Swift。在http://play.elevatorsaga.com/尝试代码学习挑战 而且有些行为很难掌握。在下面的代码中,我设置了floor&电梯对象。我正试图让电梯到达它即将通过的按钮请求(如果有人按下了那个楼层的向上或向下按钮来拨打电梯) - 在代码console.log(" (x) passing_floor - Same direction requested");

然而,我得到的日志告诉我up / downRequests未定义

passing_floor 2 up
upRequest:  undefined
downRequest:  undefined

初始化的问题是什么?范围界定?实现我想做的事情的正确方法是什么?

{
init: function(elevators, floors) {
   function initializeElevator(elevator){
       elevator.on("floor_button_pressed", function(floorNum) {
           elevator.goToFloor(floorNum);
       });
       elevator.on("idle", function() {
           elevator.goToFloor(0);   
       });

       elevator.on("passing_floor", function(floorNum, direction) {
           if ((floorNum.upRequest) && (direction =='up')) {
               floorNum.upRequest = false;
               console.log(" (x) passing_floor - Same direction requested");
           } else if ((floorNum.downRequest) && (direction == 'down')) {
               floorNum.downRequest = false;
               console.log(" (x) passing_floor - Same direction requested");
           } else {
               console.log("passing_floor " + floorNum + " " + direction);
               console.log("upRequest:  "   + floorNum.upRequest);
               console.log("downRequest:  " + floorNum.downRequest);
           }
       });   
   }

    function initializeFloor(floor){
        var upRequest = false;
        var downRequest = false;
        floor.on("up_button_pressed", function() {
             this.upRequest = true;
        });
        floor.on("down_button_pressed", function() {
            this.downRequest = true;
        });
    }

elevators.forEach(initializeElevator);
floors.forEach(initializeFloor); 

},
update: function(dt, elevators, floors) { }
}

感谢您抽出宝贵时间帮助我更多地了解Javascript,尝试W3School来解决它,但如果您有更好的网站,请告诉我,我应该看看..

1 个答案:

答案 0 :(得分:0)

我能够通过创建一个全局的“阵列”来实现所需的行为 - 我不确定这是否是一个范围问题或其他问题!

{
    init: function(elevators, floors) {
        var floorArray = new Array(floors.length); 
        var elevatorOnFloor = new Array(floors.length);


        //the init function populates the listeners for the elevator objects
       function initializeElevator(elevator){
           elevator.on("floor_button_pressed", function(floorNum) {
               elevator.goToFloor(floorNum, true);
           });
           elevator.on("idle", function() {
               elevator.goToFloor(0);   
           });

           elevator.on("passing_floor", function(floorNum, direction) { 
               if ((floorArray[floorNum] == 1) && (direction =='up')) {
                   floorArray[floorNum] = 0;
                   elevator.goToFloor(floorNum, true);  
                   console.log("picking someone else going UP");
                   elevatorOnFloor[floorNum]+=1;
                   console.log("elevators per floor:"+ elevatorOnFloor );

               } else if ((floorArray[floorNum] = 2) && (direction == 'down')) {
                   floorArray[floorNum] = 0;
                   elevator.goToFloor(floorNum, true);  
                   console.log("picking someone else going DOWN");
                   elevatorOnFloor[floorNum]-=1;
                   console.log("elevators per floor:" + elevatorOnFloor + floorArray );
               } else {
                   console.log("passing_floor " + floorNum + " " + direction);
                   if (direction =='up') {
                   elevatorOnFloor[floorNum]+=1;
                   } else {
                       elevatorOnFloor[floorNum]-=1;
                   }
               }


           });




       }








        function initializeFloor(floor){
            floor.on("up_button_pressed", function() {
                 floorArray[floor] = 1;
            });
            floor.on("down_button_pressed", function() {
                 floorArray[floor] = 2;
            });
        }
        function initializeElevatorsOnFloor(floor){
            elevatorOnFloor[floor] = 0;
        }



    // we initialize all the elevators    
    elevators.forEach(initializeElevator);
    // initialize the floors
    floors.forEach(initializeFloor);
    floors.forEach(initializeElevatorsOnFloor);

    },
    update: function(dt, elevators, floors) {
        // We normally don't need to do anything here
    }
}