I want to do this without writing the code X times. How you do that in JavaScript ? I am from Java.
floors[0].on("up_button_pressed", function() {
elevator.goToFloor(floors[0].floorNum());
} );
floors[0].on("down_button_pressed", function() {
elevator.goToFloor(floors[0].floorNum());
} );
floors[1].on("up_button_pressed", function() {
elevator.goToFloor(floors[1].floorNum());
} );
floors[1].on("down_button_pressed", function() {
elevator.goToFloor(floors[1].floorNum());
} );
floors[2].on("up_button_pressed", function() {
elevator.goToFloor(floors[2].floorNum());
} );
floors[2].on("down_button_pressed", function() {
elevator.goToFloor(floors[2].floorNum());
} );
floors[3].on("up_button_pressed", function() {
elevator.goToFloor(floors[3].floorNum());
} );
floors[3].on("down_button_pressed", function() {
elevator.goToFloor(floors[3].floorNum());
} );
floors[4].on("up_button_pressed", function() {
elevator.goToFloor(floors[4].floorNum());
} );
floors[4].on("down_button_pressed", function() {
elevator.goToFloor(floors[4].floorNum());
} );
3 Answers 3
First of all, in
floors[0].on("up_button_pressed", function () {
elevator.goToFloor(floors[0].floorNum());
});
the floors[0] inside the function is always the same object that the one that is triggering the event, so I would change it to use this:
floors[0].on("up_button_pressed", function () {
elevator.goToFloor(this.floorNum());
});
and then, just do it for all floors with forEach:
floors.forEach(function (floor) {
floor.on("up_button_pressed", function () {
elevator.goToFloor(this.floorNum());
});
// same for "down_button_pressed" here
});
Since handler functions do not need to access outer variable floor and use this, you can even define them out of loop and use them by name, if you wished:
function onFloorUp() {
elevator.goToFloor(this.floorNum());
}
// same for onFloorUp
floors.forEach(function (floor) {
floor.on("up_button_pressed", onFloorUp);
// same for "down_button_pressed" here
});
Comments
You can just make use of Array.forEach and refactor your code to the below
floors.forEach(function(floor){
floor.on('up_button_pressed,down_button_pressed', function(){
elevator.goToFloor(floor.floorNum());
});
});
You're not doing anything specific when when up_button_pressed or down_button_pressed is triggered, so just combine those events using a comma.
Comments
here we go... first you take one of your function that you need to be iterated.
floors[0].on("up_button_pressed", function() {
elevator.goToFloor(floors[0].floorNum());
} );
then you transform that code so that code can work in iteration (for example for loop)
for(var i=0; i<x; i++){
floors[i].on("up_button_pressed", function(i) {
elevator.goToFloor(floors[i].floorNum());
}, i ); }
I do that on casperjs, but I think that will be work on regular javascript too...(because casperjs is javascript too)
onevent? There should be one event listener and the event should contain the information which button was pressed on which floor. Then the elevator can go to that floor.